2014-10-24 1 views
0

사용자 정의 렌더러에있는 Xamarin/Android 맵에 이미지 오버레이를 추가하려고합니다.Monodroid/Xamarin으로지도에 오버레이 그리기

지도에 표시되지 않습니다. 이 요리법이 오래된 것 같지 않습니다. http://developer.xamarin.com/recipes/android/controls/mapview/add_an_overlay_to_a_map/ 또는 Android.GoogleMaps.Overlay 클래스가 사라졌습니다. Android.GoogleMaps.Overlay이 입니다

public class MapContentPageRenderer: PageRenderer 
    { 
     Android.Views.View _view; 
     private GoogleMap _map; 
     private MapFragment _mapFragment; 
     private LatLng _initLatLng; 

     protected override void OnElementChanged(ElementChangedEventArgs<Page> e) 
     { 
      base.OnElementChanged(e); 

      var page = e.NewElement as MapContentPage; 
      var activity = this.Context as Activity; 

      if (activity != null) 
      { 
       _view = activity.LayoutInflater.Inflate(Resource.Layout.MapLayout, this, false); 
       AddView(_view); 
       if (page != null) _initLatLng = new LatLng(page.InitLat, page.InitLng); 
       InitMapFragment(activity); 
      } 
      SetupMapIfNeeded(); 
     } 

     protected override void OnLayout(bool changed, int l, int t, int r, int b) 
     { 
      base.OnLayout(changed, l, t, r, b); 
      var msw = MeasureSpec.MakeMeasureSpec(r - l, MeasureSpecMode.Exactly); 
      var msh = MeasureSpec.MakeMeasureSpec(b - t, MeasureSpecMode.Exactly); 
      if (_view == null) return; 
      _view.Measure(msw, msh); 
      _view.Layout(0, 0, r - l, b - t); 
     } 

     private void InitMapFragment(Activity activity) 
     { 
      var cameraPosition = new CameraPosition.Builder().Target(_initLatLng).Zoom(14.0f).Build(); 
      _mapFragment = activity.FragmentManager.FindFragmentByTag("mapWithOverlay") as MapFragment; 
      if (_mapFragment != null) return; 
      var mapOptions = new GoogleMapOptions() 
       .InvokeMapType(GoogleMap.MapTypeNormal) 
       .InvokeRotateGesturesEnabled(false) 
       .InvokeZoomControlsEnabled(false) 
       .InvokeCamera(cameraPosition) 
       .InvokeCompassEnabled(true); 

      var fragTx = activity.FragmentManager.BeginTransaction(); 
      _mapFragment = MapFragment.NewInstance(mapOptions); 

      fragTx.Add(Resource.Id.mapWithOverlay, _mapFragment, "mapWithOverlay"); 
      fragTx.Commit(); 
     } 

     private void SetupMapIfNeeded() 
     { 
      if (_map != null) return; 
      _map = _mapFragment.Map; 
      if (_map == null) return; 
      ZoomToPosition(); 

      var mapOverlay = new GroundOverlayOptions() 
       .Position(_initLatLng, 150, 200) 
       .InvokeImage(BitmapDescriptorFactory.FromResource(Resource.Drawable.Icon)); 
      _map.AddGroundOverlay(mapOverlay); 
     } 

     private void ZoomToPosition() 
     { 
      var cameraUpdate = CameraUpdateFactory.NewLatLngZoom(_initLatLng, 15); 

      _map.MoveCamera(cameraUpdate); 
     } 
    } 
+0

궁금한 점은 null을 확인하는 데있어 무엇이 좋을까요? 이미 완료된 작업을 피하려고 노력하고 있습니까? – Pete

+1

나에게 그것은 현재의 접근법에서지도가 만들어 질 때 안정적으로 말할 수 없다는 것이 이상하게 보입니다. OnMapLoadedCallback 또는 무엇인가를 구현할 수있는 솔루션이 있어야한다고 생각합니다. 나는 꽤 안드로이드에 익숙하지 않다.'InitMapFragment'라는 메서드가 있다면'null' 값을 검사하는 것에 대해 아무 것도하지 않는다. 완전히 초기화 된 맵을 가질 것이라고 기대하지만, ive를 읽었을 때'fragTx . 커밋'이 끝났습니다. – BillPull

답변

관련 문제