首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Android中用图标更改xamarin.forms.maps标记

如何在Android中用图标更改xamarin.forms.maps标记
EN

Stack Overflow用户
提问于 2021-05-29 14:51:11
回答 1查看 546关注 0票数 1

我想用图标改变xamarin.forms.maps上的当前标记。

在我的项目中,我使用以下代码创建了CustomMap.cs类和CustomPin.cs类:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using Xamarin.Forms.Maps;

namespace MaritsaTundzhaForecast
{
    public class CustomMap : Map
    {
        public List<CustomPin> CustomPins { get; set; }
    }
}

using System;
using Xamarin.Forms.Maps;

namespace MaritsaTundzhaForecast
{
    public class CustomPin : Pin
    {
        public string Name { get; set; }
        public string Url { get; set; }
    }
}

之后,我用以下代码在xaml页面上设置了映射:

代码语言:javascript
复制
 <Grid>
  <Label Text="Forecast Maritza-Tundzha"
         HorizontalOptions="CenterAndExpand"
         FontSize="Large"
         FontAttributes="Bold"/>
    <Grid Margin="0,30,0,10">
        <Grid.RowDefinitions>
            <RowDefinition Height="400" />
        </Grid.RowDefinitions>

      <local:CustomMap x:Name="customMap"
               MapType="Street" />
    </Grid>
</Grid>

在cs文件中的同一个xaml页面中,我用以下代码在地图上设置了标记:

代码语言:javascript
复制
 public MainPage()
    {
        InitializeComponent();

        CustomPin pin = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(37.79752, -122.40183),
            Label = "Xamarin San Francisco Office",
            Address = "394 Pacific Ave, San Francisco CA",
            Name = "Xamarin",
            Url = "http://xamarin.com/about/"
        };
        customMap.CustomPins = new List<CustomPin> { pin };
        customMap.Pins.Add(pin);
        customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0)));
    }

现在我读了这个示例,我不知道如何以我的方式使用这个示例中的代码:

代码语言:javascript
复制
    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.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
    return marker;
}

因此,我在drawable文件夹中设置了图标。我可以从这段代码中更改什么来使用不同的图标,以及是否需要在另一个类中更改代码?

А会尝试用另一个图标替换标准图标,结果失败了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-29 16:39:07

正如杰森所说,在您提供的链接中有完整的示例应用程序。示例地图应用程序

您需要在android项目文件夹中创建一个自定义地图呈现器,如下所示。创建一个名为CustomMapRenderer的类

代码语言:javascript
复制
[assembly: ExportRenderer(typeof(CustomMap),typeof(CustomMapRenderer))]
    namespace CustomRenderer.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.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
                return marker;
            }
    
            void OnInfoWindowClick(object sender, GoogleMap.InfoWindowClickEventArgs e)
            {
                var customPin = GetCustomPin(e.Marker);
                if (customPin == null)
                {
                    throw new Exception("Custom pin not found");
                }
    
                if (!string.IsNullOrWhiteSpace(customPin.Url))
                {
                    var url = Android.Net.Uri.Parse(customPin.Url);
                    var intent = new Intent(Intent.ActionView, url);
                    intent.AddFlags(ActivityFlags.NewTask);
                    Android.App.Application.Context.StartActivity(intent);
                }
            }
    
            public Android.Views.View GetInfoContents(Marker marker)
            {
                var inflater = Android.App.Application.Context.GetSystemService(Context.LayoutInflaterService) as Android.Views.LayoutInflater;
                if (inflater != null)
                {
                    Android.Views.View view;
    
                    var customPin = GetCustomPin(marker);
                    if (customPin == null)
                    {
                        throw new Exception("Custom pin not found");
                    }
    
                    if (customPin.Name.Equals("Xamarin"))
                    {
                        view = inflater.Inflate(Resource.Layout.XamarinMapInfoWindow, null);
                    }
                    else
                    {
                        view = inflater.Inflate(Resource.Layout.MapInfoWindow, null);
                    }
    
                    var infoTitle = view.FindViewById<TextView>(Resource.Id.InfoWindowTitle);
                    var infoSubtitle = view.FindViewById<TextView>(Resource.Id.InfoWindowSubtitle);
    
                    if (infoTitle != null)
                    {
                        infoTitle.Text = marker.Title;
                    }
                    if (infoSubtitle != null)
                    {
                        infoSubtitle.Text = marker.Snippet;
                    }
    
                    return view;
                }
                return null;
            }
    
            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;
            }
        }
    }

如您所见,您在此呈现中提到的代码部分。您可以在这里更改图标。

代码语言:javascript
复制
    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);

///<---- Change icon here--->
        marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.pin));
        return marker;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67752708

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档