2014-12-05 3 views
0

Xamarin.iOS에서 Google Maps SDK를 사용하여 장치의 현재 위치를 얻으려고하고 있는데 다음 코드가 있지만 예외가 발생합니다.Xamarin iOS Google지도 SDK - 예외 (mapView.AddObserver())

나는이 내 코드입니다

using Google.Maps; 

자 마린에서 iOS 용 Google지도 SDK를 가져온 : 다음

MapView gmapView; 
public override void ViewDidLoad() 
{ 
    base.ViewDidLoad(); 

    CameraPosition camera = CameraPosition.FromCamera (latitude: 40.7056308, 
      longitude: -73.9780035, 
      zoom: 13); 
    mapView = MapView.FromCamera (new RectangleF(0, 0, View.Frame.Width, View.Frame.Bottom - 50), camera); 
    mapView.Settings.CompassButton = true; 
    mapView.Settings.MyLocationButton = true; 

    // Listen to the myLocation property of GMSMapView. 
    mapView.AddObserver (this, new NSString ("myLocation"), NSKeyValueObservingOptions.New, IntPtr.Zero); 

    // More code ... 
    // ... 

    // Ask for My Location data after the map has already been added to the UI. 
    InvokeOnMainThread (()=> mapView.MyLocationEnabled = true); 
} 

나는 호출하는 메소드를 정의 할 때 사용자의 위치 변경 :

public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) 
{ 
    if (!firstLocationUpdate) { 
     // If the first location update has not yet been recieved, then jump to that 
     // location. 
     firstLocationUpdate = true; 
     var location = change.ObjectForKey (NSValue.ChangeNewKey) as CLLocation; 
     mapView.Camera = CameraPosition.FromCamera (location.Coordinate, 14); 
    } 
} 

하지만 다음과 같은 예외가 발생하며이 문제에 봉착했습니다. RA는 행운과 동시에 :/

MonoTouch.Foundation.MonoTouchException: Objective-C exception thrown. Name: NSInternalInconsistencyException Reason: <MapViewController: 0x1b6648c0>: An - observeValueForKeyPath:ofObject:change:context: message was received but not handled. 

답변

1

시도이 같은이 라인 InvokeOnMainThread 내부

mapView.AddObserver (this, new NSString ("myLocation"), NSKeyValueObservingOptions.New, IntPtr.Zero); 

을, 이동 :

InvokeOnMainThread (()=> { 
    mapView.MyLocationEnabled = true; 
    mapView.AddObserver (this, new NSString ("myLocation"), NSKeyValueObservingOptions.New, IntPtr.Zero); 
});` 
0

저도 같은 문제로 실행했습니다이었다 모든 것을 시도했지만 아무것도 도움이되지 않았습니다. Xamarin의 Google지도 라이브러리에 버그가 있습니다.

마지막으로 observertimer으로 다시 작성했습니다. 못 생겼지 만 지금은 효과가 있습니다.

NSRunLoop.Main.AddTimer (NSTimer.CreateRepeatingTimer (0.2d, (timer) => { 
    var location = mapView.MyLocation; 

    if (location == null) { 
     return; 
    } else { 
     timer.Invalidate(); 
    } 

    //do what you need with current location 

}), NSRunLoopMode.Default); 
관련 문제