how to draw a route on map with polyline in xamarine with c#

Yairk_kaufmann 6 Reputation points
2021-11-03T17:48:39.56+00:00

I want to make an app that shows routes from place to place and I wanted to draw the route, how can I do that?

Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Maps;

namespace TKM
{
public partial class MainPage : ContentPage
{
Map mape = new Map();

    public MainPage()
    {
        InitializeComponent();
        //Content = mapi;

    }

    public async Task<Position> GetAddressLocation(string addressF , string addressT)
    {
        Geocoder geoCoder = new Geocoder();
        System.Collections.Generic.IEnumerable<Position> approximateLocations = await geoCoder.GetPositionsForAddressAsync(addressF);
        Position position1 = approximateLocations.FirstOrDefault();

        System.Collections.Generic.IEnumerable<Position> approximateLocations2 = await geoCoder.GetPositionsForAddressAsync(addressT);
        Position position2 = approximateLocations2.FirstOrDefault();

        // instantiate a polyline
        Polyline polyline = new Polyline
        {
            StrokeColor = Color.Blue,
            StrokeWidth = 2,
            Geopath =
            {
                position1,
                position2
            }
        };

        // add the polyline to the map's MapElements collection
        mapi.MapElements.Add(polyline);

        mapi.MoveToRegion(new MapSpan(mapi.VisibleRegion.Center, position1.Latitude, position1.Longitude));

        return position1;
    }

    private Polyline polyline;


    private  async void Button_Clicked(object sender, EventArgs e)
    {


        Geocoder geoCoder = new Geocoder();
        System.Collections.Generic.IEnumerable<Position> approximateLocations = await geoCoder.GetPositionsForAddressAsync(editor.Text);
        Position position1 = approximateLocations.FirstOrDefault();

        System.Collections.Generic.IEnumerable<Position> approximateLocations2 = await geoCoder.GetPositionsForAddressAsync(editori.Text);
        Position position2 = approximateLocations2.FirstOrDefault();



        // instantiate a polyline
        Polyline polyline = new Polyline
        {
            StrokeColor = Color.Blue,
            StrokeWidth = 2,

        };

        polyline.Geopath.Add(position1);
        polyline.Geopath.Add(position2);

        // add the polyline to the map's MapElements collection
        mapi.MapElements.Add(polyline);



        Pin pin = new Pin
        {
            Label = "Hod hasharon",
            Type = PinType.Place,
            Position = position1
        };

        Pin pin2 = new Pin
        {

            Label = "cfar sava",
            Type = PinType.Place,
            Position = position2

        };

        mapi.Pins.Add(pin);
        mapi.Pins.Add(pin2);

        mapi.MoveToRegion(new MapSpan(position1, position1.Latitude, position1.Longitude));

        //GetAddressLocation(editor.Text, editori.Text);
        //editori.Text = "h";
    }
}

}

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,376 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,273 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.