如何在Xamarin.Forms中单击按钮时从地图中删除折线

时间:2017-04-04 08:39:36

标签: c# xamarin.android xamarin.forms

我正在使用CustomMapRenderer.csExtendedMap.cs在地图上使用点按(触摸事件)绘制polylines

ExtendedMap.cs位于MapTap (Portable)项目中,而CustomMapRenderer.cs位于Visual Studio 2015解决方案中的MapTap.Android项目中。

CustomMapRenderer.cs

using System.Collections.Generic;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using MapTap;
using MapTap.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
using System;

[assembly: ExportRenderer(typeof(ExtendedMap), typeof(CustomMapRenderer))]
[assembly: Dependency(typeof(CustomMapRenderer))]

namespace MapTap.Droid
{
    public class CustomMapRenderer : MapRenderer, IOnMapReadyCallback,ClearPolyline
    {
        GoogleMap map;
        List<LatLng> polygontest = new List<LatLng>();

        public CustomMapRenderer()
        {
        }

        protected override void OnElementChanged(Xamarin.Forms.Platform.Android.ElementChangedEventArgs<Map> e)
        {          
            if (map != null)
                map.MapClick -= googleMap_MapClick;
            base.OnElementChanged(e);
            if (Control != null)
                ((MapView)Control).GetMapAsync(this);
        }

        public void OnMapReady(GoogleMap googleMap)
        {
            map = googleMap;
            if (map != null)
                map.MapClick += googleMap_MapClick; ;
        }

        private void googleMap_MapClick(object sender, GoogleMap.MapClickEventArgs e)
        {
            polygontest.Add(new LatLng(e.Point.Latitude, e.Point.Longitude));
            ((ExtendedMap)Element).OnTap(new Position(e.Point.Latitude, e.Point.Longitude));           

            if (polygontest.Count >1 )
            {
                AddPolygons();               
            }
        }
        public void Clearline()
        {
           map.Clear();
        }

        void AddPolygons()
        {
           var polygonOptions = new PolylineOptions();

            polygonOptions.InvokeColor(0x66FF0000);

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

            map.AddPolyline(polygonOptions);
            polygontest.RemoveAt(0);
        }   
    }
}

ExtendedMap.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms.Maps;
using Xamarin.Forms.Maps.Android;
using Android.Gms.Maps;
using Android.Gms.Maps.Model;
using Xamarin.Forms;

namespace MapTap
{   
    public class ExtendedMap : Map
    {
        public event EventHandler<TapEventArgs> Tap;
        public ExtendedMap()
        {            
        }

        public ExtendedMap(MapSpan region) : base(region)
        {
        }

        public void OnTap(Position coordinate)
        {
            OnTap(new TapEventArgs { Position = coordinate });

            var lat = coordinate.Latitude;
            var lng = coordinate.Longitude;

            putpinonclick(lat,lng);
        }

        public void putpinonclick(double lat, double lng)
        {
            var pin = new Pin
            {
                Type = PinType.SavedPin,
                Position = new Position(lat, lng),
                Label = "Xamarin San Francisco Office",
                Address = "394 Pacific Ave, San Francisco CA"
            };

            this.Pins.Add(pin);           
        }

        protected virtual void OnTap(TapEventArgs e)
        {
            var handler = Tap;
            if (handler != null) handler(this, e);
        }
    }

    public class TapEventArgs : EventArgs
    {
        public Position Position { get; set; }
    }
}

我想从MainPage.xaml

按钮点击(清除按钮)清除折线

以下是正在运行的应用程序的屏幕截图: Screenshot

我使用

CustomMapRendrer.cs调用MainPage.Xaml.cs的函数
 var test = DependencyService.Get<ClearPolyline>();
                test.Clearline();

ClearPolyLine是接口函数中的接口和Clearline,在CustomMapRender

中实现

问题在于,当我调用上面的代码行时,变量test本身不是null,但是它的所有成员变量(在CustomMapRenderer中实现)都是null。 test的类型为ClearPolyline

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

PolyLine addedLine = map.AddPolyline(polygonOptions); 这应返回添加的PolyLine,以便将此Ployline存储在属性或集合中。 在按钮上单击或命令打扰ExtendedMap的RemovePolyline property(自定义可绑定属性)并在扩展渲染器中监听属性更改并基于值调用addedLine.remove();或在折线集合中循环并将其删除