因为我使用了我的定位按钮和这个方法...
[mapView.settings setMyLocationButton:YES];
//AddObserverForDriverCurrentLocation
[mapView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:NULL];
[self.view addSubview:mapView];
dispatch_async(dispatch_get_main_queue(), ^{
mapView.myLocationEnabled = YES;
});
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray<CLLocation *> *)locations
{
NSLog(@"Locations:%@",[locations lastObject]);
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"myLocation"]) {
CLLocation *location = [object myLocation];
NSLog(@"Location, %@,", location);
CLLocationCoordinate2D target =
CLLocationCoordinate2DMake(22.7,75.8);
[mapView animateToLocation:target];
[mapView animateToZoom:17];
}
}
但是当我点击 myLocation 按钮时,它没有更新我的位置,请帮忙
最佳答案
试试这个
要移动到当前位置,您必须:
mapView.myLocationEnabled = YES;
然后您可以在您的 viewWillAppear
方法中设置一个键值观察器,然后您可以使用 GoogleMaps SDK 的位置管理器更新您的位置。
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Implement here to check if already KVO is implemented.
...
[mapView addObserver:self forKeyPath:@"myLocation" options: NSKeyValueObservingOptionNew context: nil]
}
然后观察属性。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
{
[mapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude: mapView.myLocation.coordinate.latitude
longitude: mapView.myLocation.coordinate.longitude
zoom: mapView.projection.zoom]]; // check that the zoom property once
}
}
不要忘记在 viewWillDisappear 中注销您的观察者。
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Implement here if the view has registered KVO
...
[mapView removeObserver:self forKeyPath:@"myLocation"];
}
关于ios - mylocationButton 为什么不更新我的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34963174/