【问题标题】:map pins is never drew in my android (xamarin forms)在我的 android (xamarin 表单) 中从未绘制过地图图钉
【发布时间】:2017-09-09 20:07:11
【问题描述】:

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

[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

【问题讨论】:

  • 地图控件的布局大小发生变化时,是否真的调用了VisibleRegion
  • 真不知道什么时候叫
  • VisibleRegion 应该被称为控件变化的宽度/高度。
  • 大声笑....现在我也很困惑...尝试通过旋转设备来强制重新布局,这应该会强制改变地图的 VisibleRegion ....看看你是否得到然后更改 VisibleRegion 属性....
  • 你在哪里将 isDrawn 设置为 false?在教程中看起来更好

标签: c# android google-maps xamarin xamarin.forms


【解决方案1】:

我在 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);
    }

【讨论】:

  • 我会试试的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
相关资源
最近更新 更多