2016-01-23 50 views
1

因爲我已經用我的位置按鈕,此方法...mylocationButton不更新我的位置爲什麼?

[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按鈕點擊它是不是uspdating我的位置,請幫助

+0

面臨什麼問題.... –

+0

我mylocation按鈕沒有更新我的當前location.When我移動地圖和點擊mylocation按鈕它不拽我回到我的當前位置在地圖上。 –

+0

把斷點,並檢查一次的方法被調用或不 –

回答

1

試試這個

要移到您現在的位置,你必須:

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"]; 
} 
+0

安布感謝所有的幫助,我真的很喜歡你的知識,並且你正在幫助其他人使用它..很快再次回覆你:] –

+0

tanx很多如果我的答案是有希望的,請提出答案,這對未來有用 –

相關問題