Hello guys
I am creating an app that uses the Xamarin.Forms.Maps package with a custom renderer in order to show a route starting from my current position to a specific point. It turns out that after asking for permission, if i just hardcode some coordinates, the map is shown correctly with a route tracked on it but as soon as I try to get some more information, for example awaiting a call to an api like google directions, my route is not shown anymore on the map.
What am I doing wrong? Could it be, that the map is shown before all my coordinates are loaded and passed to the map? In that case, how do i force the map to reload?
Here is a sample of my actual code:
async void GetPermissions()
{
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.LocationAlways);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.LocationAlways))
{
Device.BeginInvokeOnMainThread(async () =>
{
await DisplayAlert("Need location", "I need your location to get a route", "OK");
});
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.LocationAlways);
status = results[Permission.LocationAlways];
}
var meetingPoint = await App.RestService.GetResponse<MapPosition>("/offers/" + product.Uuid + "/meetingpoint");
if (meetingPoint.MyPosition.Latitude != 0 && meetingPoint.MyPosition.Longitude != 0)
{
var position = new Position(meetingPoint.MyPosition.Latitude, meetingPoint.MyPosition.Longitude);
var pin = new Pin
{
Type = PinType.Place,
Position = position,
Label = "Meeting for " + product.Title,
Address = "Bla bla bla bla bla"
};
MyMap.Pins.Add(pin);
}
else
{
MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(47.4972279, 8.7271877),
Xamarin.Forms.Maps.Distance.FromKilometers(1)));
}
if (status == PermissionStatus.Granted)
{
MyMap.IsShowingUser = true;
double lat = 0;
double lng = 0;
try
{
var location = await Geolocation.GetLastKnownLocationAsync();
if (location != null)
{
lat = location.Latitude;
lng = location.Longitude;
}
}
catch (Exception)
{
await DisplayAlert("Location Error", "I couldn't load your current location. Try again.", "OK");
return;
}
//...... some irrelevant code ......
if (meetingPoint.MyPosition.Latitude != 0 && meetingPoint.MyPosition.Longitude != 0)
{
var geoLocation = await App.RestService.GetExtResponse<GeoLocation>(
"LINK_TO_GOOGLE_MAPS_API");
if (geoLocation.Status.Equals("OK"))
{
// get all coordinates
// var startLocation = geoLocation.Routes[0].Legs[0].Steps[0].Start_Location;
foreach (Step step in geoLocation.Routes[0].Legs[0].Steps)
{
var polyline = GooglePoints.Decode(step.Polyline.Points);
foreach (var l in polyline)
{
// HERE I ADD MY COORDINATES TO THE MAP
// RouteCoordinates is a list of Positions, as suggested in the Xamarin Docs
MyMap.RouteCoordinates.Add(new Position(l.Latitude, l.Longitude));
}
}
var bounds = new List<Position>()
{
new Position(geoLocation.Routes[0].Bounds.NorthEast.Lat, geoLocation.Routes[0].Bounds.NorthEast.Lng),
new Position(geoLocation.Routes[0].Bounds.SouthWest.Lat, geoLocation.Routes[0].Bounds.SouthWest.Lng)
};
MyMap.MoveToRegion(MapSpan.FromCenterAndRadius(Tools.GetCentralGeoCoordinate(bounds),
Xamarin.Forms.Maps.Distance.FromMeters(geoLocation.Routes[0].Legs[0].Distance.Value / 2)));
}
else
{
//...... some irrelevant code ......
}
}
}
else if (status != PermissionStatus.Unknown)
{
//...... some irrelevant code ......
}
}
catch (Exception)
{
//...... some irrelevant code ......~~~~
}
}
The CustomMapRenderer is just copy pasted from the xamarin docs.