2013-06-28 96 views
5

我有一個GMSMapView中正確加載和我的ViewController內工作GMSMapView中myLocation沒有給實際位置

什麼我不能夠做的是周圍設置我的位置

的GMSCameraPosition這是我的代碼:

mapView_.myLocationEnabled = YES; 
CLLocation* myLoc = [mapView_ myLocation]; 
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:myLoc.coordinate.latitude 
                 longitude:myLoc.coordinate.longitude 
                  zoom:4]; 
[mapView_ setCamera:camera]; 

啓用GPS和應用程序所需的所有權限,但myLocation返回零CLLocation,必然cameraWithLatitude:longitude:zoom:得到0 0座標和顯示,而不是我的實際位置非洲(不在AFR ICA :))

回答

-4

嘗試使用MapKit,下VieDidLoadViewWillApper使用:

myMapView.showsUserLocation = YES; 

你可以,如果你想在IBAction爲下添加這個簡單的代碼,如:

- (IBAction)getLocation:(id)sender{ 

     myMapView.showsUserLocation = YES; 

} 

我希望這能幫到你

+0

我需要使用谷歌地圖,並MapKit和GMS以任何方式一起工作 – Zerho

+0

但使用谷歌地圖mapKit,我想,但不知道有這樣一個「showUserLocation」或「getUserLocation」,那麼同樣的方法你可以添加縮放和'coordinate.latitude'和'經度' – BlackSheep

12

從官方Google Maps iOS SDK文檔重刑:

  • (BOOL)myLocationEnabled我的位置點和精度圓是否啓用[讀,寫,分配] 控制。

默認爲NO。

  • (CLLocation *)myLocation [讀,分配] 如果我的位置已啓用,揭示出用戶的位置點被繪製在哪裏。

如果它被禁用,或者它被啓用但沒有位置數據可用,這將是零。這個屬性是使用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... 
} 

以下是關於如何使用KVO更新用戶位置的谷歌地圖示例代碼的代碼。

// in viewDidLoad method... 
// Listen to the myLocation property of GMSMapView. 
    [mapView_ addObserver:self 
      forKeyPath:@"myLocation" 
       options:NSKeyValueObservingOptionNew 
       context: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 that 
    // location. 
    firstLocationUpdate_ = YES; 
    CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey]; 
    mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate 
                zoom:14]; 
    } 
} 
+0

是你的第一個LocationUpdate_變量只是一個布爾屬性? – kevinl

+0

@kevinl是的,實際上,第二個代碼片段是從谷歌示例代碼複製... – Jing

+2

不能相信谷歌沒有在GMSMapViewDelegate中包含一種方法,使用KVO在這種情況下有點... –

相關問題