protected override void Dispose(bool disposing) { if (disposing) { if (Map != null) { Map.OnSnapshot -= OnSnapshot; foreach (var logic in _logics) { logic.Unregister(NativeMap, Map); } } _cameraLogic.Unregister(); _uiSettingsLogic.Unregister(); var mkMapView = (MapView)Control; if (mkMapView != null) { mkMapView.CoordinateLongPressed -= CoordinateLongPressed; mkMapView.CoordinateTapped -= CoordinateTapped; mkMapView.CameraPositionChanged -= CameraPositionChanged; mkMapView.DidTapMyLocationButton = null; } } base.Dispose(disposing); }
protected override async void OnElementChanged(ElementChangedEventArgs <Map> e) { base.OnElementChanged(e); var oldMapView = (MapControl)Control; if (e.OldElement != null) { var mapModel = e.OldElement; mapModel.OnSnapshot -= OnSnapshot; _cameraLogic.Unregister(); _uiSettingsLogic.Unregister(); if (oldMapView != null) { oldMapView.ActualCameraChanged -= OnActualCameraChanged; oldMapView.ZoomLevelChanged -= OnZoomLevelChanged; oldMapView.CenterChanged -= Control_CenterChanged; } } if (e.NewElement != null) { var mapModel = e.NewElement; if (Control == null) { SetNativeControl(new MapControl()); Control.MapServiceToken = FormsGoogleMaps.AuthenticationToken; Control.TrafficFlowVisible = Map.IsTrafficEnabled; Control.ZoomLevelChanged += OnZoomLevelChanged; Control.CenterChanged += Control_CenterChanged; Control.ActualCameraChanged += OnActualCameraChanged; } _cameraLogic.Register(Map, NativeMap); Map.OnSnapshot += OnSnapshot; _uiSettingsLogic.Register(Map, NativeMap); _uiSettingsLogic.Initialize(); UpdateMapType(); UpdateHasScrollEnabled(); UpdateHasZoomEnabled(); UpdateHasRotationEnabled(); await UpdateIsShowingUser(Element.MyLocationEnabled); foreach (var logic in _logics) { logic.Register(oldMapView, e.OldElement, NativeMap, Map); logic.RestoreItems(); logic.OnMapPropertyChanged(new PropertyChangedEventArgs(Map.SelectedPinProperty.PropertyName)); } } }
protected override void Dispose(bool disposing) { if (disposing) { _cameraLogic.Unregister(); foreach (var logic in _logics) { logic.Unregister(NativeMap, Map); } var mkMapView = (MapView)Control; mkMapView.CoordinateLongPressed -= CoordinateLongPressed; mkMapView.CoordinateTapped -= CoordinateTapped; mkMapView.CameraPositionChanged -= CameraPositionChanged; mkMapView.DidTapMyLocationButton = null; } base.Dispose(disposing); }
private void Uninitialize(Android.Gms.Maps.GoogleMap nativeMap, Map map) { try { if (nativeMap == null) { System.Diagnostics.Debug.WriteLine($"Uninitialize failed - {nameof(nativeMap)} is null"); return; } if (map == null) { System.Diagnostics.Debug.WriteLine($"Uninitialize failed - {nameof(map)} is null"); return; } _uiSettingsLogic.Unregister(); map.OnSnapshot -= OnSnapshot; _cameraLogic.Unregister(); foreach (var logic in _logics) { logic.Unregister(nativeMap, map); } if (Map.IsUseCluster) { _clusterLogic.Unregister(nativeMap, map); } nativeMap.SetOnMapClickListener(null); nativeMap.SetOnMapLongClickListener(null); nativeMap.SetOnMyLocationButtonClickListener(null); nativeMap.MyLocationEnabled = false; nativeMap.Dispose(); } catch (System.Exception ex) { var message = ex.Message; System.Diagnostics.Debug.WriteLine($"Uninitialize failed. - {message}"); } }
protected override async void OnElementChanged(ElementChangedEventArgs <View> e) { base.OnElementChanged(e); // For XAML Previewer or FormsGoogleMaps.Init not called. if (!FormsGoogleMaps.IsInitialized) { var tv = new TextView(Context) { Gravity = GravityFlags.Center, Text = "Xamarin.Forms.GoogleMaps" }; tv.SetBackgroundColor(Color.Teal.ToAndroid()); tv.SetTextColor(Color.Black.ToAndroid()); SetNativeControl(tv); return; } var oldMapView = (MapView)Control; var mapView = new MapView(Context); mapView.OnCreate(s_bundle); mapView.OnResume(); SetNativeControl(mapView); var activity = Context as Activity; if (activity != null) { var metrics = new DisplayMetrics(); activity.WindowManager.DefaultDisplay.GetMetrics(metrics); foreach (var logic in _logics) { logic.ScaledDensity = metrics.ScaledDensity; } _cameraLogic.ScaledDensity = metrics.ScaledDensity; } if (e.OldElement != null) { var oldMapModel = (Map)e.OldElement; _cameraLogic.Unregister(); var oldGoogleMap = await oldMapView.GetGoogleMapAsync(); if (oldGoogleMap != null) { oldGoogleMap.SetOnCameraChangeListener(null); oldGoogleMap.SetOnMapClickListener(null); oldGoogleMap.SetOnMapLongClickListener(null); oldGoogleMap.SetOnMyLocationButtonClickListener(null); } oldMapView.Dispose(); } if (oldMapView != null) { _oldNativeMap = await oldMapView.GetGoogleMapAsync(); _oldMap = (Map)e.OldElement; } NativeMap = await((MapView)Control).GetGoogleMapAsync(); _cameraLogic.Register(Map, NativeMap); OnMapReady(NativeMap); }