Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Xamarin Forms Maps - how to refresh/update the map - CustomMap Renderer

$
0
0

I search a lot but I still have the same problem:

How can I update, refresh or reload the Xamarin.Forms.Maps?

In the class definition (class CustomMap : Map), there is no method to update the maps.. Maybe a MVVM logic can solves the problem, but I can't find it on the Web..

I followed this tutorial for the maps : Working with maps

To customise it, I followed this tutorial : Highlight a Route on a Map

So, after these tutorials (I made the same things, no changes), I tried with 2 RouteCoordinates which gave me a straight line... I then made an algorithm which works perfectly.

DirectionMap

    public class DirectionMap
    {
    public Distance distance { get; set; }
    public Duration duration { get; set; }
    public Address address_start { get; set; }
    public Address address_end { get; set; }
    public List<Step> steps { get; set; }

    public class Distance
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    public class Duration
    {
        public string text { get; set; }
        public int value { get; set; }
    }
    public class Address
    {
        public string text { get; set; }
        public Position position { get; set; }
    }
    public class Step
    {
        public Position start { get; set; }
        public Position end { get; set; }
    }
}

ResponseHttpParser

public static void parseDirectionGoogleMapsResponse(HttpStatusCode httpStatusCode, JObject json, Action<DirectionMap, string> callback)
{
    switch (httpStatusCode)
    {
        case HttpStatusCode.OK:

            DirectionMap directionMap = null;
            string strException = null;

            try
            {
                directionMap = new DirectionMap()
                {
                    distance = new DirectionMap.Distance()
                    {
                        text = (json["routes"][0]["legs"][0]["distance"]["text"]).ToString(),
                        value = Int32.Parse((json["routes"][0]["legs"][0]["distance"]["value"]).ToString())
                    },
                    duration = new DirectionMap.Duration()
                    {
                        text = (json["routes"][0]["legs"][0]["duration"]["text"]).ToString(),
                        value = Int32.Parse((json["routes"][0]["legs"][0]["duration"]["value"]).ToString())
                    },
                    address_start = new DirectionMap.Address()
                    {
                        text = (json["routes"][0]["legs"][0]["start_address"]).ToString(),
                        position = new Position(Double.Parse((json["routes"][0]["legs"][0]["start_location"]["lat"]).ToString()), Double.Parse((json["routes"][0]["legs"][0]["start_location"]["lng"]).ToString()))
                    },
                    address_end = new DirectionMap.Address()
                    {
                        text = (json["routes"][0]["legs"][0]["end_address"]).ToString(),
                        position = new Position(Double.Parse((json["routes"][0]["legs"][0]["end_location"]["lat"]).ToString()), Double.Parse((json["routes"][0]["legs"][0]["end_location"]["lng"]).ToString()))
                    }
                };

                bool finished = false;
                directionMap.steps = new List<Step>();
                int index = 0;

                while (!finished)
                {
                    try
                    {
                        Step step = new Step()
                        {
                            start = new Position(Double.Parse((json["routes"][0]["legs"][0]["steps"][index]["start_location"]["lat"]).ToString()), Double.Parse((json["routes"][0]["legs"][0]["steps"][index]["start_location"]["lng"]).ToString())),
                            end = new Position(Double.Parse((json["routes"][0]["legs"][0]["steps"][index]["end_location"]["lat"]).ToString()), Double.Parse((json["routes"][0]["legs"][0]["steps"][index]["end_location"]["lng"]).ToString()))
                        };
                        directionMap.steps.Add(step);
                        index++;
                    }
                    catch (Exception e)
                    {
                        finished = true;
                    }
                }
            }
            catch (Exception e)
            {
                directionMap = null;
                strException = e.ToString();
            }
            finally
            {
                callback(directionMap, strException);
            }
            break;
        default:
            switch (httpStatusCode)
            {

            }
            callback(null, json.ToString());
            break;
    }
}

I just get the distance and duration for some private calculs and get each step that I put into a List<>;

When everything is finished, I use my callback which bring us back to the controller (MapPage.xaml.cs the XAML Form Page (Xamarin Portable))

Now, everything becomes weird.. It's like the map doesn't get that changes are made

public partial class MapPage : ContentPage
{
public MapPage()
   {
    InitializeComponent();
    setupMap();
    setupMapCustom();
}

public void setupMapCustom()
{
    customMap.RouteCoordinates.Add(new Position(37.785559, -122.396728));
    customMap.RouteCoordinates.Add(new Position(37.780624, -122.390541));
    customMap.RouteCoordinates.Add(new Position(37.777113, -122.394983));
    customMap.RouteCoordinates.Add(new Position(37.776831, -122.394627));

    customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Xamarin.Forms.Maps.Distance.FromMiles(1.0)));
}       

public async void setupMap()
{
    customMap.MapType = MapType.Satellite;

    string origin = "72100 Le Mans";
    string destination = "75000 Paris";

    HttpRequest.getDirections(origin, destination, callbackDirections);

    customMap.RouteCoordinates.Add(await MapUtilities.GetMapPointOfStreetAddress(origin));
    Position position = await MapUtilities.GetMapPointOfStreetAddress(destination);
    //customMap.RouteCoordinates.Add(position);

    var pin = new Pin
    {
        Type = PinType.Place,
        Position = position,
        Label = "Destination !!",
    };
    customMap.Pins.Add(pin);
}

private async void callbackDirections(Object obj, string str)
{
    if (obj != null)
    {
        DirectionMap directionMap = obj as DirectionMap;

        foreach (Step step in directionMap.steps)
        {
            customMap.RouteCoordinates.Add(step.start);
            System.Diagnostics.Debug.WriteLine("add step");
        }

        customMap.RouteCoordinates.Add(directionMap.address_end.position);
        System.Diagnostics.Debug.WriteLine("add last step");
    }
    else
    {
        System.Diagnostics.Debug.WriteLine(str);
    }
}

I run my app, everything works until it's something fast, because of the time spent by my algorithm etc, the callback is coming too late and then I need to refresh, reload or update my map... Anyway, I need to update my map in the future, so... If anyone can help, this one is welcome !

PS: Take a look at my post on Xamarin Forms Maps - how to refresh/update the map - CustomMap Renderer, maybe someone answered since I post this problem !


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>