protected override void OnDataContextChanged(object previousContext, object newContext) { var previousAnnotations = MapView.Annotations.Where(a => !(a is MKUserLocation)).ToArray(); var isFirstLoading = previousAnnotations.Length == 0; MapView.RemoveAnnotations(previousAnnotations); if (DataContext != null && DataContext.HasAddresses()) { var annotations = DataContext.Addresses .Select(a => new MapViewAnnotation( DataContext.Name.GetAbbreviation(2), DataContext.Name, a)) .ToArray(); var coordinates = MapUtil.GetAnnotationsCoordinates(annotations); if (coordinates.Length > 0) { MapView.SetRegion( MapUtil.CoordinateRegionForCoordinates( coordinates, new MKMapSize(3000, 3000)), !isFirstLoading); MapView.SetCenterCoordinate( new CLLocationCoordinate2D( annotations[0].Coordinate.Latitude + 0.00047, // moving a bit down to compensate callout height annotations[0].Coordinate.Longitude), !isFirstLoading); MapView.AddAnnotations(annotations); } AddressLabel.Text = DataContext.GetAddressText(); } else { AddressLabel.Text = null; } SetNeedsUpdateConstraints(); }
private void InitializeMapView() { if (ViewModel.OrgEvent != null && ViewModel.OrgEvent.Venues != null) { if (!_isMapViewInitialized) { VenuesMapView.RemoveAnnotations(VenuesMapView.Annotations); if (!(VenuesMapView.WeakDelegate is MapDelegate)) { var mapDelegate = new MapDelegate { SelectAnnotationCommand = ViewModel.SelectVenueOnMapCommand, ShowDetailsCommand = ViewModel.NavigateVenueCommand }; VenuesMapView.Delegate = mapDelegate; } var annotations = ViewModel.OrgEvent.Venues .SelectMany(v => v.Info.Addresses .Select(a => new VenueAnnotation(v, a))).ToArray(); var coordinates = MapUtil.GetAnnotationsCoordinates(annotations); VenuesMapView.SetRegion(MapUtil.CoordinateRegionForCoordinates(coordinates, MapMargin), false); VenuesMapView.AddAnnotations(annotations); if (ViewModel.SelectedVenueOnMap != null) { SelectVenueMapAnnotation(ViewModel.SelectedVenueOnMap); } _isMapViewInitialized = true; } } else { VenuesMapView.RemoveAnnotations(VenuesMapView.Annotations); _isMapViewInitialized = false; } }
private void SelectVenueMapAnnotation(Venue venue) { if (venue != null) { var annotation = GetAnnotationByVenue(venue); if (annotation != null) { VenuesMapView.SelectAnnotation(annotation, true); } } else if (VenuesMapView.SelectedAnnotations != null && VenuesMapView.SelectedAnnotations.Any()) { foreach (var annotation in VenuesMapView.SelectedAnnotations) { VenuesMapView.DeselectAnnotation(annotation, false); } var annotations = VenuesMapView.Annotations.OfType <VenueAnnotation>(); var coordinates = MapUtil.GetAnnotationsCoordinates(annotations); VenuesMapView.SetRegion(MapUtil.CoordinateRegionForCoordinates(coordinates, MapMargin), true); } }
private void SelectAnnotation() { MapViewControl.RemoveAnnotations(MapViewControl.Annotations); if (ViewModel.Annotation != null && ViewModel.Annotation.Addresses != null && ViewModel.Annotation.Addresses.Length > 0) { var annotations = ViewModel.Annotation.Addresses .Select(address => new MapViewAnnotation( ViewModel.Annotation.Pin, ViewModel.Annotation.Title, address)).ToArray(); var coordinates = MapUtil.GetAnnotationsCoordinates(annotations); MapViewControl.SetRegion( MapUtil.CoordinateRegionForCoordinates( coordinates, new MKMapSize(5000, 5000)), false); MapViewControl.AddAnnotations(annotations); MapViewControl.SelectAnnotation(annotations[0], false); } }