gpt4 book ai didi

c# - map 别针从未在我的 android 中绘制(xamarin 表单)

转载 作者:行者123 更新时间:2023-11-30 00:21:55 26 4
gpt4 key购买 nike

这是我的 mapRenderer,我需要在 Android 中绘制图钉,但出于某些原因,我的 if (e.PropertyName.Equals("VisibleRegion") && !isDrawn) 永远不会为真。 ..然后,我的 map 图钉永远不会被画出来有人知道这是真的吗?问题是什么?我正在使用 xamarin 表单,这是在我的 android 项目中...关于 map 的所有其余部分都运行良好

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace neoFly_Montana.Droid
{
public class CustomMapRenderer : MapRenderer, IOnMapReadyCallback
{
GoogleMap map;
List<Position> routeCoordinates;
List<CustomPin> customPins;
Position userLocation;
bool isDrawn;

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;
//pin
customPins = formsMap.CustomPins;
routeCoordinates = formsMap.RouteCoordinates;
userLocation = formsMap.userLocation;

((MapView)Control).GetMapAsync(this);
}
}

public void OnMapReady(GoogleMap googleMap)
{
map = googleMap;

var polylineOptions = new PolylineOptions();
polylineOptions.InvokeColor(0x66FF0000);

foreach (var position in routeCoordinates)
{
polylineOptions.Add(new LatLng(position.Latitude, position.Longitude));
}

map.AnimateCamera(CameraUpdateFactory.NewLatLngZoom(new LatLng(userLocation.Latitude, userLocation.Longitude), 15.0f));
map.AddPolyline(polylineOptions);
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);

if (e.PropertyName.Equals("VisibleRegion") && !isDrawn)
{
NativeMap.Clear();
//NativeMap.InfoWindowClick += OnInfoWindowClick;
//NativeMap.SetInfoWindowAdapter(this);

foreach (var pin in customPins)
{
var marker = new MarkerOptions();
marker.SetPosition(new LatLng(pin.Pin.Position.Latitude, pin.Pin.Position.Longitude));
marker.SetTitle(pin.Pin.Label);
marker.SetSnippet(pin.Pin.Address);
marker.SetIcon(BitmapDescriptorFactory.FromResource(Resource.Drawable.menusmiley));

NativeMap.AddMarker(marker);
}
isDrawn = true;
}
}
}

永远不会是“VisibleRegion”的 e.PropertyName

最佳答案

我在 OnMapReady 中显示我的图钉

public void OnMapReady(GoogleMap googleMap)
{
_googleMap = googleMap;

if (_googleMap != null)
{
_googleMap.MapClick += OnMapClick;
UpdatePins();
}
}

private void UpdatePins(bool firstUpdate = true)
{
if (_googleMap == null) return;

if (FormsMap.CustomPins != null)
{
foreach (var pin in FormsMap.CustomPins)
{
AddPin(pin);
}

if (firstUpdate)
{
var observable = FormsMap.CustomPins as INotifyCollectionChanged;
if (observable != null)
{
observable.CollectionChanged += OnCustomPinsCollectionChanged;
}
}
}
}

private void AddPin(CustomPin pin)
{
var markerWithIcon = new MarkerOptions();

markerWithIcon.SetPosition(new LatLng(pin.BasePin.Position.Latitude, pin.BasePin.Position.Longitude));

if (!string.IsNullOrWhiteSpace(pin.BasePin.Label))
markerWithIcon.SetTitle(pin.BasePin.Label);

_googleMap.AddMarker(markerWithIcon);
}

关于c# - map 别针从未在我的 android 中绘制(xamarin 表单),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46134689/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com