[英]How to add KML-file routes to a Xamarin.Forms.Android google map?
I have some KML-files with routes that I wish to add to my map on my xamarin.forms.android project. 我在xamarin.forms.android项目中有一些带有路由的KML文件,希望将其添加到地图中。 I have created a custom renderer looking like this but now I am unsure on how to add the local kml-files (that i have in my resources folder) to the map.
我已经创建了一个自定义渲染器,如下所示,但是现在我不确定如何将本地kml文件(我在资源文件夹中)添加到地图中。 ( https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/polyline-map-overlay/ )
( https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/custom-renderer/map/polyline-map-overlay/ )
Class: 类:
public class CustomMap : Map
{
public List<Position> RouteCoordinates { get; set; }
public CustomMap()
{
RouteCoordinates = new List<Position>();
}
}
Renderer: 渲染:
GoogleMap map;
List<Position> routeCoordinates;
protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
// Unsubscribe
}
if (e.NewElement != null)
{
var formsMap = (CustomMap)e.NewElement;
routeCoordinates = formsMap.RouteCoordinates;
((MapView)Control).GetMapAsync(this);
}
}
public 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);
}
And this is how i use it if i want to try a route (customMap is the map i created in XAML): 如果我想尝试路线,这就是我使用它的方式(customMap是我在XAML中创建的地图):
customMap.RouteCoordinates.Add (new Position (37.785559, -122.396728));
customMap.RouteCoordinates.Add (new Position (37.780624, -122.390541));
But I want the KML-file to be added on the map and automatically create the route. 但我希望将KML文件添加到地图上并自动创建路线。
How would I add the KML-files to my renderer now? 我现在如何将KML文件添加到渲染器?
You can use the Google Maps Android API Utility Library (via an Xamarin Binding library) to add/remove KML layers to your map: 您可以使用Google Maps Android API实用程序库(通过Xamarin绑定库)将KML图层添加/删除到地图中:
The KML can be supplied as a resource id or a stream, for the work that I do I use stream from downloaded files in the cache directory: KML可以作为资源ID或流提供,对于我要做的工作,我使用缓存目录中下载文件中的流:
var kmlLayer = new KmlLayer(googleMap, kmlfileStream, ApplicationContext);
kmlLayer.AddLayerToMap();
Re: https://developers.google.com/maps/documentation/android-api/utility 回复: https : //developers.google.com/maps/documentation/android-api/utility
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.