【问题标题】:Xamarin - How to show Routes using Google Maps APIXamarin - 如何使用 Google Maps API 显示路线
【发布时间】:2018-12-25 04:43:56
【问题描述】:

我想在我的应用中显示两个位置之间的路线。我有一个 Google Maps API,但我不知道如何从 Google 获取信息。我读过,我需要从谷歌调用一个链接,然后我成为 json 中的代码。但是我怎么能编码呢?你能帮我提供任何代码吗...

【问题讨论】:

    标签: xamarin xamarin.forms xamarin.ios xamarin.android xamarin-studio


    【解决方案1】:

    1.您需要实现 POLYLINE 连接线段序列创建为单个对象。您可以创建直线段、弧段或两者的组合

    2.当您调用 google api 时,您会得到点,您需要使用这些点在地图上绘制折线(路径)

    3.你需要在下面的代码中自定义渲染你的地图有点相同

     [assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
    namespace MyAndroid.Droid
    {
    public class CustomMapRenderer : MapRenderer
    {
        List<Position> routeCoordinates;
    
        public CustomMapRenderer(Context context) : base(context)
        {
        }
    
        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;
                Control.GetMapAsync(this);
            }
        }
    
        protected override void OnMapReady(Android.Gms.Maps.GoogleMap map)
        {
            base.OnMapReady(map);
    
            var polylineOptions = new PolylineOptions();
            polylineOptions.InvokeColor(0x66FF0000);
    
            foreach (var position in routeCoordinates)
            {
                polylineOptions.Add(new LatLng(position.Latitude, position.Longitude));
            }
    
            NativeMap.AddPolyline(polylineOptions);
        }
    }
    

    }

    关注this示例供参考

    【讨论】:

    • 感谢您的回答,但我没有这样的经验单独做这件事:(
    • 那么有人打电话给开发人员,你需要雇用他们,然后他们可能会帮助你..
    【解决方案2】:

    假设您使用的是 Xamarin.Forms.Maps,并且您已完成所有设置,以下是移动地图或设置图钉的示例:

      Maps.MoveToRegion(MapSpan.FromCenterAndRadius(
                                  new Position(-23.4859591, -47.4420192),
                                  Distance.FromMiles(0.5)));
    
    
                var pin = new Pin
                {
                    Type = PinType.Place,
                    Position = new Position(-23.4859591, -47.4420192),
                    Label = "Maps",
                    Address = "www.abc.com",
                };
    
                Maps.Pins.Add(pin);
    

    【讨论】:

    • 感谢您的回答,但这不是我的问题。
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多