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

标签 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 按钮时,它没有更新我的位置,请帮忙

最佳答案

试试这个

要移动到当前位置,您必须:

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/

相关文章:

ios - 检查应用程序是否已购买

ios - 将深度数据(16 位深度)作为辅助数据存储在 JPEG 文件中

ios - 使用单个 NSPredicate 过滤多个数组

ios - 如何使用蓝牙经典而不是 LE

objective-c - xcode 8 错误类不可用使用 self 代替

objective-c - 如何在 uitableview 中显示从 avplayer 播放的 ipodLibrary 中的选定歌曲

iOS10.3.x 无法在 MPMusicPlayer 上为 Apple Music 轨道设置 nowPlayingItem

objective-c - 突出显示 CPT 散点图的部分

iphone - 是否有 "Find My iPhone?"的 iOS 配置文件 key

iphone - 如何在 iPhone 上打开音乐(ipod 库)?