首页 > 解决方案 > 为 Android 刷新地图加载

问题描述

我正在开发一个用户必须发现地方的应用程序。我正在使用它来显示地图上未发现地点的区域: https ://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map/circle-map-overlay

但后来随着应用程序功能的增长,它不再运行良好。这是我的github的链接。 https://github.com/jakuboles/TDKInz2020/tree/master/TDK

用户登录后,TDK.Android.CustomMapRenderer.OnElementChanged 函数在 TDK.MainPage.xaml.cs.DisplayInMap 之前被调用的速度更快,我在其中设置了哪些地方应该显示为已发现,哪些为未发现。结果,现在应用程序始终位于 TDK.Android.CustomMapRenderer.OnElementChanged 第 36 行位置列表始终为空,并且调用 OnMapReady 绘制圆圈列表为空。

当 DisplayInMap 将位置分配给列表时,是否有某种方法可以再次调用 OnElementChanged?或者让 OnElementChanged 稍后运行,在 DisplayInMap 之后会设置列表。

很抱歉,如果这是一个复杂的描述;不知道如何正确描述。

这是我的 Android 渲染器:

using Android.Content;
using Android.Gms.Maps.Model;
using Java.Lang;
using MapOverlay;
using MapOverlay.Droid;
using System.Collections.Generic;
using TDK.MapsCustoms;
using Xamarin.Forms;
using Xamarin.Forms.Maps.Android;

[assembly: ExportRenderer(typeof(CustomMap), typeof(CustomMapRenderer))]
namespace MapOverlay.Droid
{
    public class CustomMapRenderer : MapRenderer
    {
        List<CustomCircle> circles;

        public CustomMapRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Xamarin.Forms.Maps.Map> e)
        {
            base.OnElementChanged(e);



            if (e.OldElement != null)
            {

            }

            if (e.NewElement != null)
            {
                var formsMap = (CustomMap)e.NewElement;
                circles = formsMap.CircleList;
            }
        }

        protected override void OnMapReady(Android.Gms.Maps.GoogleMap map)
        {
            base.OnMapReady(map);

            foreach (var circle in circles)
            {
                var circleOptions = new CircleOptions();
                circleOptions.InvokeCenter(new LatLng(circle.Position.Latitude, circle.Position.Longitude));
                circleOptions.InvokeRadius(circle.Radius);
                circleOptions.InvokeFillColor(0X66FF0000);
                circleOptions.InvokeStrokeColor(0X66FF0000);
                circleOptions.InvokeStrokeWidth(0);

                NativeMap.AddCircle(circleOptions);
            }
        }
    }
}

标签: c#xamarinxamarin.formsxamarin.android

解决方案


移动线

circles = formsMap.CircleList;

OnElementChangedOnMapReady


推荐阅读