void OnMapLongPress()
        {
            if (longPressGestureRecognizer.State != UIGestureRecognizerState.Began)
            {
                return;
            }

            CGPoint pixelLocation             = longPressGestureRecognizer.LocationInView(NativeMapView);
            CLLocationCoordinate2D coordinate = NativeMapView.ConvertPoint(pixelLocation, NativeMapView);

            //add map annotation
            MKPointAnnotation annotation = new MKPointAnnotation();

            annotation.Coordinate = coordinate;
            annotation.Title      = string.Format("{0},{1}", coordinate.Latitude, coordinate.Longitude);

            NativeMapView.AddAnnotation(annotation);
            annotationsList.Add(annotation);
        }
Beispiel #2
0
        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs <Xamarin.Forms.Maps.Map> e)
        {
            try
            {
                base.OnElementChanged(e);
            }
            catch (Java.Lang.SecurityException exception)
            {
                //Newer versions of android OS allow users to set location permissions at runtime which can cause a security exception
                //we will catch it here and continue as we check for the appropriete permissions when needed
                System.Diagnostics.Debug.WriteLine(exception.Message);
            }

            if (e.OldElement != null)
            {
                if (googleMap != null)
                {
                    googleMap.MapLongClick    -= OnMapLongClick;
                    googleMap.InfoWindowClick -= OnMapMarkerInfoWindowClick;
                }

                CustomMap oldCustomMap = e.OldElement as CustomMap;
                oldCustomMap.CalculateRouteFromUserLocationNativeHandler -= CalculateRouteFromUserLocation;
                oldCustomMap.CalculateRouteNativeHandler        -= CalculateRoute;
                oldCustomMap.ClearRouteNativeHandler            -= ClearRoute;
                oldCustomMap.CenterOnUsersLocationNativeHandler -= CenterOnUsersLocation;
                oldCustomMap.SearchLocalNativeHandler           -= SearchLocal;
            }

            if (e.NewElement != null)
            {
                CustomMap newCustomMap = e.NewElement as CustomMap;
                newCustomMap.CalculateRouteFromUserLocationNativeHandler += CalculateRouteFromUserLocation;
                newCustomMap.CalculateRouteNativeHandler        += CalculateRoute;
                newCustomMap.ClearRouteNativeHandler            += ClearRoute;
                newCustomMap.CenterOnUsersLocationNativeHandler += CenterOnUsersLocation;
                newCustomMap.SearchLocalNativeHandler           += SearchLocal;

                //UpdateShowTraffic();

                NativeMapView.GetMapAsync(this);
            }
        }
        void CalculateRouteFromUserLocation(object sender, EventArgs e)
        {
            CLLocationCoordinate2D destinationLocation  = new CLLocationCoordinate2D(CustomMapView.RouteDestination.Latitude, CustomMapView.RouteDestination.Longitude);
            MKPlacemark            destinationPlacemark = new MKPlacemark(coordinate: destinationLocation);

            sourceMapItem      = MKMapItem.MapItemForCurrentLocation();
            destinationMapItem = new MKMapItem(destinationPlacemark);

            MKPointAnnotation destinationAnnotation = new MKPointAnnotation();

            //destinationAnnotation.Title = "Destination";
            destinationAnnotation.Coordinate = destinationPlacemark.Location.Coordinate;

            //save annotations so they can be removed later if requested
            annotationsList.Add(destinationAnnotation);

            NativeMapView.AddAnnotation(destinationAnnotation);

            CalculateRouteDetails();
        }
        protected override void OnElementChanged(Xamarin.Forms.Platform.iOS.ElementChangedEventArgs <View> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                NativeMapView.OverlayRenderer        = null;
                NativeMapView.DidUpdateUserLocation -= OnUserLocationUpdated;
                NativeMapView.GetViewForAnnotation  -= GetViewForAnnotation;
                NativeMapView.RemoveGestureRecognizer(longPressGestureRecognizer);

                CustomMap oldCustomMap = e.OldElement as CustomMap;
                oldCustomMap.CalculateRouteFromUserLocationNativeHandler -= CalculateRouteFromUserLocation;
                oldCustomMap.CalculateRouteNativeHandler        -= CalculateRoute;
                oldCustomMap.ClearRouteNativeHandler            -= ClearRoute;
                oldCustomMap.CenterOnUsersLocationNativeHandler -= CenterOnUsersLocation;
                oldCustomMap.SearchLocalNativeHandler           -= SearchLocal;
            }

            if (e.NewElement != null)
            {
                NativeMapView.OverlayRenderer        = GetOverlayRenderer;
                NativeMapView.DidUpdateUserLocation += OnUserLocationUpdated;
                NativeMapView.GetViewForAnnotation  += GetViewForAnnotation;
                NativeMapView.AddGestureRecognizer(longPressGestureRecognizer);

                CustomMap newCustomMap = e.NewElement as CustomMap;
                newCustomMap.CalculateRouteFromUserLocationNativeHandler += CalculateRouteFromUserLocation;
                newCustomMap.CalculateRouteNativeHandler        += CalculateRoute;
                newCustomMap.ClearRouteNativeHandler            += ClearRoute;
                newCustomMap.CenterOnUsersLocationNativeHandler += CenterOnUsersLocation;
                newCustomMap.SearchLocalNativeHandler           += SearchLocal;

                UpdateShowTraffic();
            }
        }
        void SearchLocal(object sender, EventArgs e)
        {
            MKLocalSearchRequest request = new MKLocalSearchRequest();

            request.NaturalLanguageQuery = CustomMapView.SearchText;
            request.Region = NativeMapView.Region;

            MKLocalSearch search = new MKLocalSearch(request);

            search.Start((MKLocalSearchResponse response, Foundation.NSError error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.Description);
                }
                else if (response.MapItems.Length == 0)
                {
                    System.Diagnostics.Debug.WriteLine("No matches found for search: " + CustomMapView.SearchText);
                }
                else
                {
                    ClearRoute(null, null);

                    foreach (MKMapItem mapItem in response.MapItems)
                    {
                        MKPointAnnotation annotation = new MKPointAnnotation();
                        annotation.Coordinate        = mapItem.Placemark.Coordinate;
                        annotation.Title             = mapItem.Name;

                        annotationsList.Add(annotation);
                    }

                    NativeMapView.ShowAnnotations(annotationsList.ToArray(), true);
                }
            });
        }
        void CalculateRouteDetails(bool zoomMapToShowFullRoute = true, bool requestAlternateRoutes = true, MKDirectionsTransportType transportType = MKDirectionsTransportType.Automobile)
        {
            MKDirectionsRequest directionRequest = new MKDirectionsRequest();

            directionRequest.Source                  = sourceMapItem;
            directionRequest.Destination             = destinationMapItem;
            directionRequest.RequestsAlternateRoutes = requestAlternateRoutes;
            directionRequest.TransportType           = transportType;

            MKDirections eta = new MKDirections(directionRequest);

            eta.CalculateETA((MKETAResponse response, Foundation.NSError error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.Description);
                }
                else
                {
                    TimeSpan time = TimeSpan.FromSeconds(response.ExpectedTravelTime);
                    System.Diagnostics.Debug.WriteLine(time.ToString(@"hh\:mm\:ss\:fff"));
                }
            });

            MKDirections directions = new MKDirections(directionRequest);


            directions.CalculateDirections((MKDirectionsResponse response, Foundation.NSError error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.Description);
                }
                else
                {
                    //remove previous route overlays
                    ClearOverlays();

                    currentRouteOverlay = response.Routes[0].Polyline;

                    MKRoute route;

                    //loop through backwards so first most route is renderered on top
                    for (int i = response.Routes.Length - 1; i >= 0; i--)
                    {
                        route = response.Routes[i];

                        //save overlay so it can be removed later if requested
                        overlaysList.Add(route.Polyline);

                        NativeMapView.AddOverlay(route.Polyline, MKOverlayLevel.AboveRoads);

                        if (i == 0)
                        {
                            if (zoomMapToShowFullRoute)
                            {
                                MKMapRect rect         = route.Polyline.BoundingMapRect;
                                MKMapRect expandedRect = NativeMapView.MapRectThatFits(rect, new UIEdgeInsets(20, 20, 20, 20));

                                NativeMapView.SetRegion(MKCoordinateRegion.FromMapRect(expandedRect), true);
                            }
                        }
                    }
                }
            });
        }