0

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

 mapView.showsUserLocation=YES;

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

我无法弄清楚。有什么帮助吗?

4

5 回答 5

1

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

当您做出决定时,请查看协议mapView:didUpdateUserLocation:方法。MKMapViewDelegate在此方法触发后,可以userLocation通过MKMapView.

更新

如果您想在已检查用户位置的情况下打开地图视图,您可以考虑使用CLLocationManagerand CLLocationManagerDelegate。通过这种方式,您可以检查位置服务是否可用,并在方法locationManager:didUpdateToLocation:fromLocation:启动后打开地图视图。

有关完整信息,请查看获取用户位置编程指南

于 2012-11-03T23:34:37.340 回答
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) 在 vi​​ewController.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) 现在在你的 UI 中添加 Mapview

注意:选择MapView并转到Attribute InspectorCKECK Mark the Shows user location Under BEHAVIOR

于 2014-06-02T12:38:02.400 回答
1

您无法在视图中获取用户位置坐标确实加载了您需要做的是使用下面的委托方法。

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

确保您的地图对象与委托连接刚刚测试它应该可以工作

于 2012-11-03T23:40:00.590 回答
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);
于 2012-11-03T23:52:09.733 回答
0

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

根据您需要在“信息属性列表”字典下添加键的方法(requestAlwaysAuthorization 或 requestWhenInUseAuthorization)。第一种方法使用:NSLocationAlwaysUsageDescription,第二种方法使用:NSLocationWhenInUseUsageDescription。

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

顶部的两个键都是我们正在寻找的。 它应该是什么样子

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

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

于 2016-05-03T16:26:27.610 回答