从官方Google Maps iOS SDK文档:
(BOOL)myLocationEnabled [read,write,assign]控制是否启用"我的位置"点和精度圆.默认为NO.
(CLLocation*)myLocation [read,assign]如果启用了"我的位置",则显示正在绘制用户位置点的位置.
如果它被禁用,或者它已启用但没有可用的位置数据,则为零.使用KVO可以观察到这个属性.
因此,当您设置时mapView_.myLocationEnabled = YES;,它只会告诉mapView您只有在给予该myLocation属性的位置值时才显示蓝点.Google的示例代码显示了如何使用KVO方法观察用户位置.(推荐)您还可以实现该CLLocationManagerDelegate方法,以更新mapView.
-(void)locationManager:(CLLocationManager*)manager didUpdateToLocation:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation
{
[mapView animateToLocation:newLocation.coordinate];// some code...
}
以下是Google地图示例代码中有关如何使用KVO更新用户位置的代码.
// in viewDidLoad method...
// Listen to the myLocation property of GMSMapView.
[mapView_addObserver:selfforKeyPath:@"myLocation"options:NSKeyValueObservingOptionNewcontext: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 tha
t// location.
firstLocationUpdate_ =YES;
CLLocation*location = [change objectForKey:NSKeyValueChangeNewKey];
mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:14];
}
}
不敢相信Google没有在GMSMapViewDelegate中包含一个方法,在这种情况下使用KVO有点骇客
网友评论