如何在ios中使用Google maps sdk获取当前位置
大家好,我是Ios初学者,在我的项目中,当用户在路上行走或开车时,我使用Google SDK获取路线 但是我无法获取当前位置,并且无法在我的x代码控制台中显示0.0和0.0(纬度和经度值)。请帮助我获取当前位置 我的代码如下:-如何在ios中使用Google maps sdk获取当前位置,ios,objective-c,google-maps,Ios,Objective C,Google Maps,大家好,我是Ios初学者,在我的项目中,当用户在路上行走或开车时,我使用Google SDK获取路线 但是我无法获取当前位置,并且无法在我的x代码控制台中显示0.0和0.0(纬度和经度值)。请帮助我获取当前位置 我的代码如下:- 获取位置是一个异步过程。因此,您需要“等待”位置被更新——在本例中,通过观察“myLocation” 观察位置 此代码仅在屏幕上显示“地图视图”时有效 // Listen to the myLocation property of GMSMapView. [_mapVi
获取位置是一个异步过程。因此,您需要“等待”位置被更新——在本例中,通过观察“myLocation” 观察位置 此代码仅在屏幕上显示“地图视图”时有效
// Listen to the myLocation property of GMSMapView.
[_mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
// Ask for My Location data after the map has already been added to the UI.
dispatch_async(dispatch_get_main_queue(), ^{
_mapView.myLocationEnabled = YES;
});
获取位置更新:
请检查此堆栈溢出流链接:请修改我的代码并发布,如果您想获取用户的当前位置,您需要使用
CLLocationManager
,您可以签出以获取更多详细信息。请参阅Google Maps IOS SDK演示中的MyLocationViewController
// Listen to the myLocation property of GMSMapView.
[_mapView addObserver:self
forKeyPath:@"myLocation"
options:NSKeyValueObservingOptionNew
context:NULL];
// 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)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];
}
}