简体   繁体   English

如何将KML文件路由添加到Xamarin.Forms.Android谷歌地图?

[英]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.

相关问题 在Xamarin Forms Android中循环出arraylist时,无法获取KML文件地标 - Unsuccessful in getting out KML-file placemarks when looping out an arraylist in Xamarin Forms Android 使用Android资源路径获取kml文件? - Get kml-file using android resource path? 使用 xamarin.forms.android 检测屏幕镜像 - Detect screen mirroring using xamarin.forms.android 如何在谷歌地图片段android studio中导入KML多边形文件 - How to import a KML polygon file in google map fragment android studio Xamarin.Forms.Android:在编译/部署时使用示例数据文件填充“Environment.SpecialFolder.Personal” - Xamarin.Forms.Android: Populating 'Environment.SpecialFolder.Personal' with sample data files at compilation/deployment Xamarin.Forms.Android:java.lang.IllegalStateException:片段没有视图 - Xamarin.Forms.Android: java.lang.IllegalStateException: Fragment does not have a view 如何在android&xamarin android中的源和目的地的Google地图上显示所有可能的路线? - How to Show all possible routes on google map for source and destination in android & xamarin android? 从我的Google Map Android导出KML文件 - export KML file from my google map android 如何在Google Map(Android片段)上显示(公开可用的,即非本地的)KML文件 - How to display (publicly available i.e. non-local) KML file on a Google Map (Android fragment) 如何在不使用 kml 的情况下向 android studio 中的 google map 应用程序添加大量多边形 - How to add a lot of polygon to a google map app in android studio without using kml
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM