【发布时间】:2020-03-18 03:29:14
【问题描述】:
我正在关注 Xamarin 的文章,该文章描述了如何使用图像自定义 pin here
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 中有一个 png 图像。但是,地图没有显示任何图钉。我在自定义渲染器中放置了一个断点,它命中了,所以我知道它被调用了。下面是实现自定义地图的页面代码:
Xaml
<controls:CustomMap MapType="Street" x:Name="map" WidthRequest="150" IsVisible="True" HasZoomEnabled="True" HasScrollEnabled="True">
<controls:CustomMap.HeightRequest>
<OnIdiom>
<OnIdiom.Phone>
<OnPlatform
iOS="250"
Android="150" />
</OnIdiom.Phone>
</OnIdiom>
</controls:CustomMap.HeightRequest>
</controls:CustomMap>
以及背后的代码
var position = new Xamarin.Forms.Maps.Position(item.Latitude.Value, item.Longitude.Value); // Latitude, Longitude
var pin = new CustomPin
{
Type = PinType.Place,
Position = position,
Label = item.Name,
Address = item.AddressString
};
pin.Clicked += async (sender, e) =>
{
await Navigation.PushAsync(new RestaurantDetails(item));
};
Device.BeginInvokeOnMainThread(() =>
{
map.Pins.Add(pin);
map.CustomPins.Add(pin);
// map.MoveToRegion(MapSpan.FromCenterAndRadius(position, Distance.FromMiles(0.3)));
});
【问题讨论】:
-
BitmapDescriptorFactory.FromResource 是否返回值?
-
是的,它返回一个值
-
解决了,没有把图片也放到drawable-xxxx文件夹里。啊!
标签: xamarin xamarin.forms