Exemplo n.º 1
0
 public NavigateMapMessage(object sender, double sourceLat, double sourceLng, double destinationLat, double destinationLng, DirectionsMode directionsMode = DirectionsMode.Driving) : base(sender)
 {
     this.SourceLat      = sourceLat;
     this.SourceLng      = sourceLng;
     this.DestinationLat = destinationLat;
     this.DestinationLng = destinationLng;
     this.DirectionsMode = directionsMode;
 }
        //private void MapControl_CenterChanged(MapControl sender, object args)
        //{
        //    foreach (var item in sender.MapElements)
        //    {
        //        if((item as MapIcon).Title == "Some label")
        //        {
        //            (item as MapIcon).Location = sender.Center;
        //        }
        //    }
        //}

        private void Pivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Piv.SelectedIndex == 0)
            {
                SelectedDirectionMode = DirectionsMode.Walking;
            }
            else if (Piv.SelectedIndex == 1)
            {
                SelectedDirectionMode = DirectionsMode.Transit;
            }
            else if (Piv.SelectedIndex == 2)
            {
                SelectedDirectionMode = DirectionsMode.Driving;
            }
        }
 public void NavigateUsingGoogleMaps(double destinationLat, double destinationLng, int zoomLevel = 1,
                                     DirectionsMode directionsMode = DirectionsMode.Driving)
 {
     //try
     //{
     //    var mode = directionsMode == DirectionsMode.Driving ? "driving" : "walking";
     //    var url = string.Format("http://maps.google.com/maps?saddr={0},{1}&daddr={2},{3}&zoom={4}&directionsmode={5}", sourceLat, sourceLng, destinationLat, destinationLng, zoomLevel, mode);
     //    Intent intent = new Intent(Intent.ActionView, Android.Net.Uri.Parse(url));
     //    StartActivity(intent);
     //}
     //catch (ActivityNotFoundException ex)
     //{
     //    Intent intent =
     //      new Intent(Intent.ActionView, Android.Net.Uri.Parse("market://details?id=com.google.android.apps.maps"));
     //    StartActivity(intent);
     //}
 }
Exemplo n.º 4
0
        /*private async void FindRouteAndDrawRoute (double sourceLat, double sourceLng, double destinationLat, double destinationLng, DirectionsMode directionsMode = DirectionsMode.Driving)
         *      {
         *  if (CLLocationManager.Status == CLAuthorizationStatus.NotDetermined ||  CLLocationManager.Status == CLAuthorizationStatus.Denied)
         *      return;
         *
         *  //get current location
         *  Geolocator locator = new Geolocator(){ DesiredAccuracy = 100};
         *  var location = await locator.GetPositionAsync(50000);
         *
         *  Console.WriteLine("Position Status: {0}", location.Timestamp);
         *  Console.WriteLine("Position Latitude: {0}", location.Latitude);
         *  Console.WriteLine("Position Longitude: {0}", location.Longitude);
         *
         *  MKPlacemark source = new MKPlacemark (new CLLocationCoordinate2D (sourceLat, sourceLng), new NSDictionary ());
         *              MKMapItem sourceItem = new MKMapItem (source);
         *
         *              desCoordinate = new CLLocationCoordinate2D (destinationLat, destinationLng);
         *              MKPlacemark destination = new MKPlacemark (new CLLocationCoordinate2D (destinationLat, destinationLng), new NSDictionary ());
         *              MKMapItem destinationItem = new MKMapItem (destination);
         *
         *              MKDirectionsRequest request = new MKDirectionsRequest ();
         *              request.Source = sourceItem;
         *              request.Destination = destinationItem;
         *              request.TransportType = directionsMode == DirectionsMode.Driving ? MKDirectionsTransportType.Automobile : MKDirectionsTransportType.Walking;
         *
         *              MKDirections direction = new MKDirections (request);
         *
         *              direction.CalculateDirections (delegate(MKDirectionsResponse response, NSError error) {
         *                      if (error == null) {
         *                              //remove all routes that has been drawn on map
         *                              if (mapView.Overlays != null && mapView.Overlays.Length != 0) {
         *                                      foreach (var overlay in mapView.Overlays) {
         *                                              mapView.RemoveOverlay (overlay);
         *                                      }
         *                              }
         *
         *                              //check if have route
         *                              if (response.Routes.Length == 0) {
         *                                      Mvx.Resolve<IMvxMessenger> ().Publish (new ToastMessage (this.ViewModel, "Cannot find the route"));
         *                              }
         *
         *                              //add new route
         *                              foreach (var route in response.Routes) {
         *                                      MKPolyline polyline = route.Polyline;
         *                                      mapView.AddOverlay (polyline);
         *
         *                                      ViewModel.TotalDistance = route.Distance;
         *                                      ViewModel.ExpectedTime = TimeSpan.FromSeconds(route.ExpectedTravelTime);
         *
         *                                      foreach (var step in route.Steps) {
         *                                              ViewModel.Routes.Add (new FlexyPark.Core.Models.RouteItem () {
         *                                                      Instruction = step.Instructions,
         *                                                      Distance = step.Distance,
         *                                                      Long = step.Polyline.Coordinate.Longitude,
         *                                                      Lat = step.Polyline.Coordinate.Latitude
         *                                              });
         *
         *                                              Console.WriteLine (step.Instructions);
         *                                              Console.WriteLine (step.Distance);
         *                                      }
         *                                      break;
         *                              }
         *
         *                      } else {
         *                              Console.WriteLine (error.LocalizedDescription);
         *                      }
         *              });
         *
         *              MKMapPoint userPoint = MKMapPoint.FromCoordinate (new CLLocationCoordinate2D (sourceLat, sourceLng));
         *              MKMapRect zoomRect = new MKMapRect (userPoint.X, userPoint.Y, 0.1, 0.1);
         *
         *              MKMapPoint annotationPoint = MKMapPoint.FromCoordinate (new CLLocationCoordinate2D (destinationLat, destinationLng));
         *              MKMapRect pointRect = new MKMapRect (annotationPoint.X, annotationPoint.Y, 0.1, 0.1);
         *
         *              zoomRect = MKMapRect.Union (zoomRect, pointRect);
         *
         *  overviewRegion = MKCoordinateRegion.FromMapRect (zoomRect);
         *  overviewRegion.Span.LatitudeDelta += 0.05;
         *  overviewRegion.Span.LongitudeDelta += 0.05;
         *
         *              StepAnnotation annotationSoure = new StepAnnotation ();
         *              annotationSoure.SetCoordinate (new CLLocationCoordinate2D (sourceLat, sourceLng));
         *              mapView.AddAnnotation (annotationSoure);
         *
         *              MKPointAnnotation annotationDest = new MKPointAnnotation ();
         *              annotationDest.SetCoordinate (new CLLocationCoordinate2D (destinationLat, destinationLng));
         *              mapView.AddAnnotation (annotationDest);
         *
         *              destinationAnnotation = annotationDest;
         *
         *  mapView.SetRegion (overviewRegion, true);
         *
         *  isShowRoute = true;
         *      }*/

        private async void FindRouteAndDrawRoute(double destinationLat, double destinationLng, DirectionsMode directionsMode = DirectionsMode.Driving)
        {
            if (CLLocationManager.Status == CLAuthorizationStatus.NotDetermined || CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                return;
            }

            //get current location
            Geolocator locator = new Geolocator()
            {
                DesiredAccuracy = 100
            };
            var location = await locator.GetPositionAsync(50000);

            Console.WriteLine("Position Status: {0}", location.Timestamp);
            Console.WriteLine("Position Latitude: {0}", location.Latitude);
            Console.WriteLine("Position Longitude: {0}", location.Longitude);

            var sourceLat = location.Latitude;
            var sourceLng = location.Longitude;

            MKPlacemark source     = new MKPlacemark(new CLLocationCoordinate2D(sourceLat, sourceLng), new NSDictionary());
            MKMapItem   sourceItem = new MKMapItem(source);

            desCoordinate = new CLLocationCoordinate2D(destinationLat, destinationLng);
            MKPlacemark destination     = new MKPlacemark(new CLLocationCoordinate2D(destinationLat, destinationLng), new NSDictionary());
            MKMapItem   destinationItem = new MKMapItem(destination);

            MKDirectionsRequest request = new MKDirectionsRequest();

            request.Source        = sourceItem;
            request.Destination   = destinationItem;
            request.TransportType = directionsMode == DirectionsMode.Driving ? MKDirectionsTransportType.Automobile : MKDirectionsTransportType.Walking;

            MKDirections direction = new MKDirections(request);

            direction.CalculateDirections(delegate(MKDirectionsResponse response, NSError error) {
                if (error == null)
                {
                    //remove all routes that has been drawn on map
                    if (mapView.Overlays != null && mapView.Overlays.Length != 0)
                    {
                        foreach (var overlay in mapView.Overlays)
                        {
                            mapView.RemoveOverlay(overlay);
                        }
                    }

                    //check if have route
                    if (response.Routes.Length == 0)
                    {
                        Mvx.Resolve <IMvxMessenger> ().Publish(new ToastMessage(this.ViewModel, "Cannot find the route"));
                    }

                    //add new route
                    foreach (var route in response.Routes)
                    {
                        MKPolyline polyline = route.Polyline;
                        mapView.AddOverlay(polyline);

                        ViewModel.TotalDistance = route.Distance;
                        ViewModel.ExpectedTime  = TimeSpan.FromSeconds(route.ExpectedTravelTime);

                        foreach (var step in route.Steps)
                        {
                            ViewModel.Routes.Add(new FlexyPark.Core.Models.RouteItem()
                            {
                                Instruction = step.Instructions,
                                Distance    = step.Distance,
                                Long        = step.Polyline.Coordinate.Longitude,
                                Lat         = step.Polyline.Coordinate.Latitude
                            });

                            Console.WriteLine(step.Instructions);
                            Console.WriteLine(step.Distance);
                        }
                        break;
                    }
                }
                else
                {
                    Mvx.Resolve <IMvxMessenger>().Publish(new ToastMessage(this.ViewModel, error.LocalizedDescription));
                }
            });

            MKMapPoint userPoint = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(sourceLat, sourceLng));
            MKMapRect  zoomRect  = new MKMapRect(userPoint.X, userPoint.Y, 0.1, 0.1);

            MKMapPoint annotationPoint = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(destinationLat, destinationLng));
            MKMapRect  pointRect       = new MKMapRect(annotationPoint.X, annotationPoint.Y, 0.1, 0.1);

            zoomRect = MKMapRect.Union(zoomRect, pointRect);

            overviewRegion = MKCoordinateRegion.FromMapRect(zoomRect);
            overviewRegion.Span.LatitudeDelta  += 0.05;
            overviewRegion.Span.LongitudeDelta += 0.05;

            StepAnnotation annotationSoure = new StepAnnotation();

            annotationSoure.SetCoordinate(new CLLocationCoordinate2D(sourceLat, sourceLng));
            mapView.AddAnnotation(annotationSoure);

            MKPointAnnotation annotationDest = new MKPointAnnotation();

            annotationDest.SetCoordinate(new CLLocationCoordinate2D(destinationLat, destinationLng));
            mapView.AddAnnotation(annotationDest);

            destinationAnnotation = annotationDest;

            mapView.SetRegion(overviewRegion, true);

            isShowRoute = true;
        }
 public void NavigateUsingNativeMap(double destinationLat, double destinationLng, int zoomLevel = 1,
                                    DirectionsMode directionsMode = DirectionsMode.Driving)
 {
 }
Exemplo n.º 6
0
        public async void NavigateUsingNativeMap(double destinationLat, double destinationLng, int zoomLevel = 1, DirectionsMode directionsMode = DirectionsMode.Driving)
        {
            if (CLLocationManager.Status == CLAuthorizationStatus.NotDetermined || CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                return;
            }

            //get current location
            Geolocator locator = new Geolocator()
            {
                DesiredAccuracy = 100
            };
            var location = await locator.GetPositionAsync(50000);

            Console.WriteLine("Position Status: {0}", location.Timestamp);
            Console.WriteLine("Position Latitude: {0}", location.Latitude);
            Console.WriteLine("Position Longitude: {0}", location.Longitude);

            var sourceLat = location.Latitude;
            var sourceLng = location.Longitude;

            var urlString = string.Format("http://maps.apple.com/?saddr={0},{1}&daddr={2},{3}", sourceLat.ParseToCultureInfo(new CultureInfo("en-US")), sourceLng.ParseToCultureInfo(new CultureInfo("en-US")), destinationLat.ParseToCultureInfo(new CultureInfo("en-US")), destinationLng.ParseToCultureInfo(new CultureInfo("en-US")));

            UIApplication.SharedApplication.OpenUrl(new NSUrl(urlString));
        }
Exemplo n.º 7
0
        public async void NavigateUsingGoogleMaps(double destinationLat, double destinationLng, int zoomLevel = 1, DirectionsMode directionsMode = DirectionsMode.Driving)
        {
            if (CLLocationManager.Status == CLAuthorizationStatus.NotDetermined)
            {
                if (locationManager != null)
                {
                    // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
                    if (locationManager.RespondsToSelector(new ObjCRuntime.Selector("requestWhenInUseAuthorization")))
                    {
                        locationManager.RequestWhenInUseAuthorization();
                    }
                    return;
                }
            }

            if (CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                string title   = (CLLocationManager.Status == CLAuthorizationStatus.Denied) ? "Location services are off" : "Location Service is not enabled";
                string message = "To use Location Service you must turn on 'While using the app' in the Location Services Settings";

                if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
                {
                    Mvx.Resolve <IMvxMessenger>().Publish(new ProgressMessage(this.ViewModel, false));

                    UIAlertView alertView = new UIAlertView(title, message, null, "Cancel", "Settings");

                    alertView.Clicked += (object sender, UIButtonEventArgs e) =>
                    {
                        if (e.ButtonIndex == alertView.CancelButtonIndex) //cancel
                        {
                            //ViewModel.CloseViewModel();
                        }
                        else
                        {
                            //go to settings
                            UIApplication.SharedApplication.OpenUrl(new NSUrl(UIApplication.OpenSettingsUrlString));
                        }
                    };

                    alertView.Dismissed += (sender, e) =>
                    {
                        alertView.Dispose();
                        alertView = null;
                    };

                    alertView.Show();
                }
                else
                {
                    // ios 7 only has two CLAuthorizationStatus : Denied and AuthorizedAlways
                    if (CLLocationManager.Status == CLAuthorizationStatus.AuthorizedAlways)
                    {
                        return;
                    }

                    Mvx.Resolve <IMvxMessenger>().Publish(new ProgressMessage(this.ViewModel, false));

                    UIAlertView alertView = new UIAlertView(title, message, null, "OK");

                    alertView.Clicked += (sender, e) =>
                    {
                        //if (e.ButtonIndex == alertView.CancelButtonIndex)
                        //ViewModel.CloseViewModel();
                    };

                    alertView.Dismissed += (sender, e) =>
                    {
                        alertView.Dispose();
                        alertView = null;
                    };

                    alertView.Show();
                }

                return;
            }

            //get current location
            Geolocator locator = new Geolocator()
            {
                DesiredAccuracy = 100
            };
            var location = await locator.GetPositionAsync(50000);

            Console.WriteLine("Position Status: {0}", location.Timestamp);
            Console.WriteLine("Position Latitude: {0}", location.Latitude);
            Console.WriteLine("Position Longitude: {0}", location.Longitude);

            var sourceLat = location.Latitude;
            var sourceLng = location.Longitude;

            var mode = directionsMode == DirectionsMode.Driving ? "driving" : "walking";

            if (UIApplication.SharedApplication.CanOpenUrl(new NSUrl("comgooglemaps://")))
            {
                // GoogleMaps is installed. Launch GoogleMaps and start navigation
                var urlString = string.Format("comgooglemaps://?saddr={0},{1}&daddr={2},{3}&zoom={4}&directionsmode={5}", sourceLat.ParseToCultureInfo(new CultureInfo("en-US")), sourceLng.ParseToCultureInfo(new CultureInfo("en-US")), destinationLat.ParseToCultureInfo(new CultureInfo("en-US")), destinationLng.ParseToCultureInfo(new CultureInfo("en-US")), zoomLevel, mode);

                UIApplication.SharedApplication.OpenUrl(new NSUrl(urlString));
            }
            else
            {
                // GoogleMaps is not installed. Launch AppStore to install GoogleMaps app
                UIApplication.SharedApplication.OpenUrl(new NSUrl("http://itunes.apple.com/us/app/id585027354"));
            }
        }