用户位置-Mapview

时间:2012-11-03 23:19:48

标签: ios mkmapview mapkit

我想在我的应用加载时找出用户位置坐标。我已经实现了以下代码,但它返回0.000

 mapView.showsUserLocation=YES;

CLLocationCoordinate2D annotationCoordinate;
annotationCoordinate.latitude=mapView.userLocation.location.coordinate.latitude;
NSLog(@"%f",annotationCoordinate.latitude);

我无法弄明白。有什么帮助吗?

5 个答案:

答案 0 :(得分:1)

首先,您应该考虑到检索用户位置需要时间。此外,用户可以禁用应用程序的位置服务,甚至在连接条件期间位置服务也可能不可用。所以你最好重新考虑你的应用程序启动程序。

当您做出决定时,请查看mapView:didUpdateUserLocation:协议的MKMapViewDelegate方法。在此方法触发后,可以通过userLocation的{​​{1}}属性获取该位置。

更新

如果您想要打开已选中用户位置的地图视图,可以考虑使用MKMapViewCLLocationManager。这样,您可以检查位置服务是否可用,并在方法CLLocationManagerDelegate启动后打开地图视图。

有关完整信息,请查看Getting the User’s Location编程指南

答案 1 :(得分:1)

您无法在视图中获取用户位置坐标确实需要执行的操作是使用下面的委托方法。

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"coordinates = %f,%f", mapView.userLocation.coordinate.latitude,
          mapView.userLocation.coordinate.longitude);
}

确保您的地图对象与代理人相关联 刚测试它应该工作

答案 2 :(得分:1)

1)添加FrameWork CoreLocation和Mapkit
2)在ViewController.h文件中

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate>
{
      CLLocationManager *locationManager;
}
@property (nonatomic, strong) IBOutlet MKMapView *mapView;

3)在viewController.m文件中

- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
locationManager = [[CLLocationManager alloc] init];
 locationManager.delegate=self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}

现在在didUpdateUserLocation

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = @"Where am I?";
point.subtitle = @"I'm here!!!";
[self.mapView addAnnotation:point];}

4)现在在您的用户界面中添加Mapview

注意:选择 MapView 并转到属性检查器 CKECK标记显示用户位置在BEHAVIOR下

答案 3 :(得分:0)

我认为你应该使用它:

#import <CoreLocation/CoreLocation.h>

然后在viewDidLoad方法中:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    [locationManager startUpdatingLocation];
    [mapView setRegion:MKCoordinateRegionMake(locationManager.location.coordinate, MKCoordinateSpanMake(0.2, 0.2))];
    mapView.showsUserLocation = YES;
    NSLog(@"%f",mapView.region.center.latitude);

答案 4 :(得分:0)

好的,所以我也设法自己解决了。没有人告诉你的是你需要在info.plist中设置一个键/值对。

根据什么方法(requestAlwaysAuthorization或requestWhenInUseAuthorization),您需要在“信息属性列表”下添加一个密钥&#39;字典。对于第一种方法使用:NSLocationAlwaysUsageDescription和第二种方法使用:NSLocationWhenInUseUsageDescription。

两个键的值字段可以设置为您希望向用户显示的任何字符串。

顶部的两个键都是我们正在寻找的。 How it should look like

现在你应该看到一个提示你确认的对话框。

PS:互联网上没有教程列出了这个过程的这一步,但是当你阅读每个方法的文档(在CLLocationManager上的requestWhenInUse)时,它确实提到没有这两个键就不会显示任何内容。