Reputation: 1543
This is my custom map (PCL)
public class CustomMap : Map
{
public List<Position> RouteCoordinates { get; set; }
public CustomMap()
{
RouteCoordinates = new List<Position>();
}
}
this is My View (PCL)
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ComoChegarView : ContentPage
{
Localizacao userLocal;
public ComoChegarView(double lat, double longi)
{
userLocal = new Localizacao();
Task.Run(async () => await getUserLocal()).Wait();
Localizacao lojaLocal = new Localizacao();
lojaLocal.latitude = lat;
lojaLocal.longitude = longi;
InitializeComponent();
//route
var customMap = new CustomMap
{
IsShowingUser = true,
HeightRequest = 100,
WidthRequest = 960,
};
Content = customMap;
customMap.RouteCoordinates.Add(new Position(userLocal.latitude, userLocal.longitude));
customMap.RouteCoordinates.Add(new Position(lojaLocal.latitude, lojaLocal.longitude));
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(userLocal.latitude, userLocal.longitude), Distance.FromMiles(1.0)));
}
and this is my CustomMapRenderer (Android)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using neoFly_Montana;
using neoFly_Montana.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Maps.Android;
using Android.Gms.Maps;
using Xamarin.Forms.Maps;
using Android.Gms.Maps.Model;
[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace neoFly_Montana.Droid
{
public class CustomMapRenderer : MapRenderer, IOnMapReadyCallback
{
GoogleMap map;
List<Position> routeCoordinates;
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe
}
void OnMapReady(GoogleMap googleMap)
{
map = googleMap;
var polylineOptions = new PolylineOptions();
polylineOptions.InvokeColor(0x66FF0000);
foreach (var position in routeCoordinates)
{
polylineOptions.Add(new LatLng(position.Latitude, position.Longitude));
}
map.AddPolyline(polylineOptions);
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
routeCoordinates = formsMap.RouteCoordinates;
((MapView)Control).GetMapAsync(this);
}
}
}
}
the OnMapReady is not called and I don't know why. I am using xamarin.forms the map works, only the line doesn't Will it draw a line that follow the streets and turn correctly or do I need set the points where it should turn? I really need your answer
Please, help me!
Upvotes: 1
Views: 48
Reputation: 16652
You've placed your OnMapReady
inside of the OnElementChanged
? Is this a typo of your question? Try to get it out of OnElementChanged
.
If the routeline doesn't show, it's possible that you didn't add the right key in Android's manifest:
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="XXXXXXXXXXXXXXX" />
To get this key, you may refer to the doc Obtaining a Google Maps API Key.
Upvotes: 1