【发布时间】:2021-05-29 14:51:11
【问题描述】:
我想用图标更改xamarin.forms.maps 上的当前标记。
在我的项目中,我使用以下代码创建 CustomMap.cs 类和 CustomPin.cs 类:
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 页面上设置地图:
<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 页面中,我使用以下代码在地图上设置了标记:
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)));
}
现在我读到了这个example,但我不知道如何以我的方式使用示例中的这段代码:
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 文件夹中。我可以从这段代码中更改什么以使用不同的图标以及是否需要更改另一个类中的代码?
我整天都在尝试用另一个图标替换标准图标但失败了。
【问题讨论】:
-
您发布的链接有一个完整的示例应用程序,可以完全满足您的需求。您是否尝试过运行示例?
标签: c# xamarin xamarin.forms maps