0

I am trying to display a route from one point to another using the Google Maps Directions API. I am currently following this stack post as a guideline:

https://stackoverflow.com/a/47189382/8065149

So far, I have been able to establish that I am in fact making calls to the API, and that I am receiving a relatively large JSON file in return. I am also receiving quite a few LatLngPoints (over 1000). You can see the code that I am using below. I used the same FnDecodePolylinePoints method as the one that is used in the answer I posted above.

public void DrawRoute()
{
    var lat1 = "42.02332";
    var long1 = "-93.66791";
    var lat2 = "42.020454";
    var long2 = "-93.60969";

    var url = "https://maps.googleapis.com/maps/api/directions/json?" +
        "origin=" + lat1 + "," + long1 +
        "&destination=" + lat2 + "," + long2 +
        "&key=" + "mykey";

    var json = "";
    using (WebClient wc = new WebClient())
    {
        json = wc.DownloadString(url);
    }

    var lstDecodedPoints = FnDecodePolylinePoints(json);
    var latLngPoints = new LatLng[lstDecodedPoints.Count];
    int index = 0;
    foreach (Android.Locations.Location loc in lstDecodedPoints)
    {
        latLngPoints[index++] = new LatLng(loc.Latitude, loc.Longitude);
    }

    DependencyService.Get<IMessage>().LongAlert("size of index: " + index);

    var polylineoption = new PolylineOptions();
    polylineoption.InvokeColor(Android.Graphics.Color.Green);
    polylineoption.Geodesic(true);
    polylineoption.Add(latLngPoints);

    try
    {
        NativeMap.AddPolyline(polylineoption);
    } catch (Exception e)
    {
        if (e.Source != null)
            Console.WriteLine("Exception source: {0}", e.Source);
        throw;
    }

}

However, I keep on getting an Unhandled Exception:, and it keeps on getting highlighted on the throw; of my try-catch.

System.NullReferenceException: Object reference not set to an instance of an object.

I don't understand why the object is considered to be null, as my JSON is returning something from the Google Maps Directions API and I am getting an absurd amount of latLngPoints. Is there a problem with the polylineoption not being able to add them in time before the code tries to add them to the map? Any help would be greatly appreciated.

Also, this might be important: I am calling my DrawRoute() method that I posted above inside the OnElementChanged method of my Custom Map Renderer. I am pretty sure I saw some other code where they put it there as well, and I am hoping that that location isn't problematic and wouldn't be causing this problem.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
cjnash
  • 1,228
  • 3
  • 19
  • 37
  • 2
    there is only one line of code in your try block, so either NativeMap is null or polylineoption is. Since you use polylineoption elsewhere, then I would verify that NativeMap has been instantiated – Jason Feb 28 '19 at 00:59
  • @Jason I don't know why that didn't come across my mind... I just passed a `GoogleMap map` in and called the polyline on that and it worked. Even though my lines that it drew were across the equator and totally not the right ones, still answered my question! Thanks! – cjnash Feb 28 '19 at 01:24

0 Answers0