mylocationButton不更新我的位置为什么?

时间:2016-01-23 11:59:38

标签: ios objective-c iphone

因为我使用了我的位置按钮和这种方法...

[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按钮时,请不要通过我们的位置来帮助我们

1 个答案:

答案 0 :(得分:1)

试试这个

要移至当前位置,您必须:

try:
    do_something()
except (ValueError, AssertionError):
    handle_error()

然后,您可以在mapView.myLocationEnabled = YES; 方法中设置键值观察器,然后使用GoogleMaps SDK的位置管理器更新您的位置信息。

viewWillAppear

然后观察房产。

- (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]
}

不要忘记在viewWillDisappear中取消注册观察者。

- (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
    }
}