【问题标题】:Show pin lables on load在加载时显示点标签
【发布时间】:2020-01-30 14:37:44
【问题描述】:

I want make something like this image我正在使用 Xamarin.Forms.GoogleMaps 插件。
我想默认显示我的 pin 标签,而不点击地图中的 pin。这可能吗?

【问题讨论】:

    标签: google-maps xamarin xamarin.forms xamarin.android xamarin.ios


    【解决方案1】:

    你好像要默认打开 Pin 的信息窗口。

    您可以使用 自定义渲染器 。并在特定平台上实现。

    在共享项目中

    创建自定义地图并固定

    using System.Collections.Generic;
    using Xamarin.Forms.Maps;
    
    namespace xxx
    {
        public class CustomMap : Map
        {
            public List<CustomPin> CustomPins { get; set; }
        }
    }
    
      
    using Xamarin.Forms.Maps;
    
    namespace xxx
    {
        public class CustomPin : Pin
        {
            public string Name { get; set; }
            public string Url { get; set; }
        }
    }
    

    在 iOS 中

    using MapKit;
    
    namespace xxx.iOS
    {
        public class CustomMKAnnotationView : MKAnnotationView
        {
            public string Name { get; set; }
    
            public string Url { get; set; }
    
            public CustomMKAnnotationView(IMKAnnotation annotation, string id)
                : base(annotation, id)
            {
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using CoreGraphics;
    using xxx;
    using xxx.iOS;
    using MapKit;
    using UIKit;
    using Xamarin.Forms;
    using Xamarin.Forms.Maps;
    using Xamarin.Forms.Maps.iOS;
    using Xamarin.Forms.Platform.iOS;
    
    [assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
    namespace xxx.iOS
    {
        public class CustomMapRenderer : MapRenderer
        {
            UIView customPinView;
            List<CustomPin> customPins;
    
            protected override void OnElementChanged(ElementChangedEventArgs<View> e)
            {
                base.OnElementChanged(e);
    
                if (e.NewElement != null)
                {
                    var formsMap = (CustomMap)e.NewElement;
                    var nativeMap = Control as MKMapView;
                    customPins = formsMap.CustomPins;
    
                   
                    //add MapLoaded  Event
                    nativeMap.MapLoaded += NativeMap_MapLoaded;
                }
            }
    
            private void NativeMap_MapLoaded(object sender, EventArgs e)
            {
                annotationView.SetSelected(true,true);
            }
    
                              
            CustomPin GetCustomPin(MKPointAnnotation annotation)
            {
                var position = new Position(annotation.Coordinate.Latitude, annotation.Coordinate.Longitude);
                foreach (var pin in customPins)
                {
                    if (pin.Position == position)
                    {
                        return pin;
                    }
                }
                return null;
            }
        }
    }
    

    在安卓中

    
    using System;
    using System.Collections.Generic;
    using Android.Content;
    using Android.Gms.Maps;
    using Android.Gms.Maps.Model;
    using Android.Widget;
    using xxx;
    using xxx.Droid;
    using Xamarin.Forms;
    using Xamarin.Forms.Maps;
    using Xamarin.Forms.Maps.Android;
    
    [assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
    namespace xxx.Droid
    {
        public class CustomMapRenderer : MapRenderer, GoogleMap.IInfoWindowAdapter
        {
            List<CustomPin> customPins;
    
            public CustomMapRenderer(Context context) : base(context)
            {
            }
    
            protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
            {
                base.OnElementChanged(e);
    
                if (e.OldElement != null)
                {
                    NativeMap.InfoWindowClick -= OnInfoWindowClick;
                }
    
                if (e.NewElement != null)
                {
                    var formsMap = (CustomMap)e.NewElement;
                    customPins = formsMap.CustomPins;
                }
            }
    
            protected override void OnMapReady(GoogleMap map)
            {
                base.OnMapReady(map);
    
                NativeMap.InfoWindowClick += OnInfoWindowClick;
                NativeMap.SetInfoWindowAdapter(this);
            }
    
            protected override MarkerOptions CreateMarker(Pin pin)
            {
                var marker = new MarkerOptions();
                marker.SetPosition(new LatLng(pin.Position.Latitude, pin.Position.Longitude));
                marker.SetTitle(pin.Label);
                marker.SetSnippet(pin.Address);           
                marker.ShowInfoWindow(); 
                marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
                return marker;
            }
    
            public Android.Views.View GetInfoWindow(Marker marker)
            {
                return null;
            }
    
            CustomPin GetCustomPin(Marker annotation)
            {
                var position = new Position(annotation.Position.Latitude, annotation.Position.Longitude);
                foreach (var pin in customPins)
                {
                    if (pin.Position == position)
                    {
                        return pin;
                    }
                }
                return null;
            }
        }
    }
    

    这里有一个sample关于自定义pin,你可以了解更多关于如何自定义信息窗口的外观。

    【讨论】:

    • 请帮助我更多(OnInfoWindowClick)我应该在这个事件中做什么@lucas
    • 好的,我希望默认打开所有标签而不点击 pin 请在这一点上帮助我@lucas
    • 我已经发布了答案,你可以查看
    • @LucasZhang-MSFT,在您的CreateMarker(Pin pin) 方法中,marker.ShowInfoWindow() 无法解析。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多