Forum Xamarin.Android
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

How made a route between 2 points on a map

Hello to everyone. I'm searching a way to make a route between 2 points (I know the latlng of both) on a map made with google api.

Anyone can help me?

Posts

  • GuyProvostGuyProvost CAMember ✭✭✭

    You could use the direction API to get the waypoints in a origin / destination path.

    https://developers.google.com/maps/documentation/directions/intro

  • GennaroLimiteGennaroLimite USMember ✭✭

    Thank u so much @GuyProvost . Do u know if is there any cose example of how can I use it?

  • GuyProvostGuyProvost CAMember ✭✭✭

    If you go there https://developers.google.com/maps/documentation/directions/intro#sample-response You'll have a simple REST call to the service and the response!

  • GennaroLimiteGennaroLimite USMember ✭✭

    I already read this guide. But I don't understan very well how add this task (the route) to my project.

    To understand what I'm saying, this is my code (where I set up the map):

    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Android.App;
    using Android.Gms.Maps;
    using Android.Gms.Maps.Model;
    using Android.OS;
    using Android.Locations;
    using Android.Widget;

    namespace AppiediAlCDN
    {
    [Activity()]
    public class Mappa : Activity, IOnMapReadyCallback, ILocationListener
    {
    private GoogleMap eMap;//Map
    private Button recentre;//Button
    private Button route;//Button
    private LatLng latlng;//Latitude and longitude of our position
    private LatLng latlng2;//CDN's center
    private LatLng _latlngService;//variable that contain the latitude and longitude of the service
    private Marker marker1;//marker of our position
    double latitudine;//latitude for the method UpdateLocation()
    double longitudine;//longitude for the method UpdateLocation()
    double lat_s;//variable for take the latitude of the service
    double lon_s;//variable for take the longitude of the service
    LocationManager locationManager;//Location Manager
    string locationProvider = string.Empty;//Location Provider

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
    
            // Create your application here
            SetContentView(Resource.Layout.mainm);
    
            //take arguments from the previous activity, that are the latitude and longitude of the choosen service
            lat_s = Intent.Extras.GetDouble("Latitude");
            lon_s = Intent.Extras.GetDouble("Longitude");
    
            //Set up the map
            SetUpMap();
    
            //Inizialize the location 
            InitializeLocationManager();
    
            //recenter button
            recentre = FindViewById<Button>(Resource.Id.recentre);
            route = FindViewById<Button>(Resource.Id.route);
    
            recentre.Click += recentre_Click;
            route.Click += route_Click;
        }
    
    
        public override void OnBackPressed()
        {
            eMap.Clear();
            this.Finish();
        }
    
        void InitializeLocationManager()
        {
            // initialise the location manager 
            locationManager = (LocationManager)GetSystemService(LocationService);
            // define its Criteria
            Criteria criteriaForLocationService = new Criteria
            {
                Accuracy = Accuracy.Coarse,
                PowerRequirement = Power.Medium
            };
            // find a location provider (GPS, wi-fi, etc.)
            IList<string> acceptableLocationProviders = locationManager.GetProviders(criteriaForLocationService, true);
            // if we have any, use the first one
            if (acceptableLocationProviders.Any())
                locationProvider = acceptableLocationProviders.First();
            else
                locationProvider = string.Empty;
        }
    
        public void OnLocationChanged(Location location)
        {
            //if location has changed
            if (location != null)
            {
                latitudine = location.Latitude;
                longitudine = location.Longitude;
                if (latitudine > 0 && longitudine > 0)
                {
                    //if the map already exists
                    if (eMap != null)
                    {
                        latlng = new LatLng(latitudine, longitudine);
                        //If already exists a marker, delete it
                        if (marker1 != null)
                        {
                            marker1.Remove();
                            marker1 = null;
                        }
                        marker1 = eMap.AddMarker(new MarkerOptions().SetPosition(latlng).SetTitle("UpdateLocation").SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.marker_icon)));
                        //eMap.MoveCamera(CameraUpdateFactory.NewLatLngZoom(latlng2, 15.7F));
                    }
                    //Toast.MakeText(ApplicationContext, string.Format("UPDATE LOCATION lat => {0} long => {1}", location.Latitude, location.Longitude), ToastLength.Long).Show();
                }
            }
        } // end onLocationChanged
    
        protected override void OnResume()
        {
            base.OnResume();
            if (locationProvider != string.Empty)
                locationManager.RequestLocationUpdates(locationProvider, 0, 0, this);   
        } // end OnResume
    
        protected override void OnPause()
        {
            base.OnPause();
            locationManager.RemoveUpdates(this);
        }// end OnPause
    
        private void SetUpMap()
        {
            if (eMap == null)
            {
                FragmentManager.FindFragmentById<MapFragment>(Resource.Id.map).GetMapAsync(this);
            }
        }
    
        public void OnMapReady(GoogleMap googleMap)
        {
            eMap = googleMap;
            //latitude and longitude of the service taken from the previous activity
            _latlngService = new LatLng(lat_s,lon_s);
    
            //service marker
            MarkerOptions options1 = new MarkerOptions()
                .SetPosition(_latlngService)
                .SetTitle("Your Service is here")
                .SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.marker_service));
            eMap.AddMarker(options1);
    
            //CDN's center
            latlng2 = new LatLng(40.857390, 14.281000);
    
            //img overlay
            GroundOverlayOptions groundOverlayOptions = new GroundOverlayOptions();
            BitmapDescriptor image = BitmapDescriptorFactory.FromResource(Resource.Drawable.cdn);
            groundOverlayOptions.InvokeImage(image);
            groundOverlayOptions.Position(latlng2, 725, 450);
            groundOverlayOptions.InvokeBearing(-7.70F);
            eMap.AddGroundOverlay(groundOverlayOptions);
    
            //Set the firt view of the CDN
            CameraPosition INIT = new CameraPosition.Builder()
                .Target(latlng2)
                .Zoom(16.2F)
                .Bearing(82F)
                .Build();
    
            eMap.AnimateCamera(CameraUpdateFactory.NewCameraPosition(INIT));
        }
    
        public void OnStatusChanged(string provider, Availability status, Bundle extras) { }
    
        public void OnProviderDisabled(string provider) { }
    
        public void OnProviderEnabled(string provider) { }
    
        void recentre_Click(object sender, EventArgs e)
        {
            CameraUpdate camera = CameraUpdateFactory.NewLatLngZoom(latlng2, 16.2F);
            eMap.MoveCamera(camera);
        }
    
        void calculate_Route()
        {
    
        }
    
        void route_Click(object sender, EventArgs e)
        {
            calculate_Route();
        }
    }
    

    }

    I already add a Button for calculate the route. The information that you gave tu me (https://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&key=YOUR_API_KEY) where and how I have to use it.

    @GuyProvost Thank u so much, and I'm very sorry for annoying u. I know that maybe for u it's a simple thing. But I'm new and I'm still learning.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello all,

    @GennaroLimite looks at this article i think it will help you ...
    http://www.appliedcodelog.com/2015/08/draw-path-between-two-location-in.html

    have a nice day

  • GennaroLimiteGennaroLimite USMember ✭✭
    edited November 2016

    Thank you too @ChristopheBERNARD.

    I'm reading the article that u gave me. It's look like what I nedd. I have a question for you. The points 1-2-3 are already been implemented. Now in which part of my code I have to location the point number 4?

    image

    Thank u so much.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    @GennaroLimite

    you need to create an httpclient request and call it in a background that's all :)
    the complete project is on github looks at the link i provide in the bottom of the article there is the link ...

    with the complete project you can see how the author implement an background task ...

    happy coding guy

  • GennaroLimiteGennaroLimite USMember ✭✭

    Thank u @ChristopheBERNARD . I'm goign forward in my project. I have only another question.

    In the Costants.cs (gitHub) there are 2 constats:
    internal static string strGoogleServerKey = "AIzaSyBK9hN_VmKJrBrENK6uRN*********";
    internal static string strGoogleServerDirKey="A*********xe1Tc8-_t6Dq6CocGdb9nN-bc08CE";

    The first one is the google map's api key, but the other key? What it should be?

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭✭

    Hello @GennaroLimite

    sorry i don't know it's not my project ... :)

    have a nice coding day

  • how to start navigation between source and destination latlong point in xamarin forms maps

  • if you have code then please share me with the complete code for naviagation on xamarin maps start navigation from your current location to destination location that you click on map to mark point for destination location then start location

Sign In or Register to comment.