Google Maps API:获取当前位置iOS的坐标

我目前正在使用我的项目中的Google Maps API。 我正在设置默认的摄像头/缩放到用户的位置。 我这样做:

@implementation ViewController{ GMSMapView *mapView_; } @synthesize currentLatitude,currentLongitude; - (void)viewDidLoad { [super viewDidLoad]; mapView_.settings.myLocationButton = YES; mapView_.myLocationEnabled = YES; } - (void)loadView{ CLLocation *myLocation = mapView_.myLocation; GMSMarker *marker = [[GMSMarker alloc] init]; marker.position = CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude); marker.title = @"Current Location"; marker.map = mapView_; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLocation.coordinate.latitude longitude:myLocation.coordinate.longitude zoom:6]; mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; self.view = mapView_; NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude); } 

然而,从那时起,这是行不通的

 NSLog(@"%f, %f", myLocation.coordinate.latitude, myLocation.coordinate.longitude); 

它返回0,0,它不给当前的位置坐标。 我怎样才能正确得到用户的坐标?

。H

 #import <CoreLocation/CoreLocation.h> @property(nonatomic,retain) CLLocationManager *locationManager; 

.M

 - (NSString *)deviceLocation { NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude]; return theLocation; } - (void)viewDidLoad { locationManager = [[CLLocationManager alloc] init]; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m [locationManager startUpdatingLocation]; } 

在这里回答。

当一个应用程序第一次启动它可能还不知道你的位置,因为它通常需要一段时间的GPS设备locking到您的位置(如果它刚刚开始),特别是如果这是第一次的应用程序运行,所以用户还没有回答提示,让应用程序访问他们的位置。 此外,似乎像地图视图刚创build时, mapView.myLocation总是空的(无或有坐标0,0)。

因此,您需要等到用户的位置已知,然后更新相机位置。

一种方式可能是使用代码在如何得到当前的位置在谷歌地图sdk iphonebuild议由Puneet,但请注意,示例代码中缺less设置位置pipe理器的细节(如设置位置pipe理器的委托),这可能是为什么它不适合你。

另一个select可能是在mapView.myLocation上使用KVO,如下所述: 关于定位我自己,一些问题

顺便说一句,在您创buildmapView之前,您正在访问mapView.myLocation ,所以位置始终为零。

我只是下载了iOS的新的GoogleMap SDK演示。 以下是我从源代码中看到的“当前位置”是如何通过KVO实现的。

 #if !defined(__has_feature) || !__has_feature(objc_arc) #error "This file requires ARC support." #endif #import "SDKDemos/Samples/MyLocationViewController.h" #import <GoogleMaps/GoogleMaps.h> @implementation MyLocationViewController { GMSMapView *mapView_; BOOL firstLocationUpdate_; } - (void)viewDidLoad { [super viewDidLoad]; GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868 longitude:151.2086 zoom:12]; mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; mapView_.settings.compassButton = YES; mapView_.settings.myLocationButton = YES; // Listen to the myLocation property of GMSMapView. [mapView_ addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:NULL]; self.view = mapView_; // Ask for My Location data after the map has already been added to the UI. dispatch_async(dispatch_get_main_queue(), ^{ mapView_.myLocationEnabled = YES; }); } - (void)dealloc { [mapView_ removeObserver:self forKeyPath:@"myLocation" context:NULL]; } #pragma mark - KVO updates - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!firstLocationUpdate_) { // If the first location update has not yet been recieved, then jump to that // location. firstLocationUpdate_ = YES; CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey]; mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:14]; } } @end 

希望它可以帮助你。

以防万一任何人想要DrDev在Swift 3中的出色答案:

 var locationManager: CLLocationManager? func deviceLocation() -> String { let theLocation: String = "latitude: \(locationManager.location.coordinate.latitude) longitude: \(locationManager.location.coordinate.longitude)" return theLocation } func viewDidLoad() { locationManager = CLLocationManager() locationManager.distanceFilter = kCLDistanceFilterNone locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters // 100 m locationManager.startUpdatingLocation() }