我一直在使用我在下面链接的 Xamarin.Forms 遵循自定义 pin 教程。我已经完成了它的实现,我也开始通过点击向地图添加图钉。
(旁注:我几乎只使用 Xamarin.Forms 和 Android)
目前,我可以点击地图,并在该位置添加一个新的自定义图钉,这很棒。不好的是,我无法弄清楚如何让轻按和长按手势工作,而不仅仅是正常的轻按。为了解决这个问题,因为无论如何我最终都必须添加这些,所以我计划添加一个按钮,用户可以按下该按钮来启动他们想要向地图添加一个按钮,稍后我将添加一个撤消按钮,还有很多其他的,等等。
问题是,我不知道如何从我用于地图的自定义渲染中获取切换按钮状态的结果。我在哪里可以获得切换按钮的结果并将其用作是否添加按钮的布尔值?
这是切换按钮代码,我从他们在此页面上的示例中逐行获取:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/button
这是我通过单击添加自定义引脚的代码:
private void GoogleMap_MapClick(object sender, GoogleMap.MapClickEventArgs e)
{
((CustomMap)Element).OnTap(new Position(e.Point.Latitude, e.Point.Longitude));
var addingPin = new CustomPin
{
Type = PinType.Place,
Position = new Position(e.Point.Latitude, e.Point.Longitude),
Address = " - need to possibly implement - ",
Id = "shelter",
Label = "Pin from tap",
Url = "http://www.redcross.org"
};
Map.Pins.Add(addingPin);
this.customPins.Add(addingPin);
}
我考虑过制作自定义按钮渲染,但据我所知,我一次只能将一个 Android 渲染应用于内容页面,当我尝试向地图渲染添加自定义按钮渲染时,这种方法并不满意,因为它正在接受某种 Android Map View 而不是按钮视图:
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;
customPins = formsMap.CustomPins;
Control.GetMapAsync(this);
}
}
任何帮助是极大的赞赏。下面我包含了我的应用程序到目前为止的样子,以及我正在使用的自定义页面。
using Hermes.Models;
using System;
using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
namespace Hermes
{
public class MapPage : ContentPage
{
public MapPage()
{
CustomMap customMap = new CustomMap()
{
HeightRequest = 100,
WidthRequest = 960,
VerticalOptions = LayoutOptions.FillAndExpand,
MapType = MapType.Street,
};
var examplePinSupplies = new CustomPin
{
Type = PinType.Place,
Position = new Position(42.02525, -93.65087),
Address = " - need to possibly implement - ",
Id = "supplies",
Label = "supplies",
Url = "https://www.redcross.org/store"
};
var examplePinMedical = new CustomPin
{
Type = PinType.Place,
Position = new Position(42.02290, -93.63912),
Address = " - need to possibly implement - ",
Id = "medical",
Label = "medical",
Url = "http://www.redcross.org"
};
var examplePinShelter = new CustomPin
{
Type = PinType.Place,
Position = new Position(42.02045, -93.60968),
Address = " - need to possibly implement - ",
Id = "shelter",
Label = "shelter",
Url = "http://www.redcross.org"
};
customMap.CustomPins = new List<CustomPin> { examplePinSupplies, examplePinMedical, examplePinShelter };
customMap.Pins.Add(examplePinSupplies);
customMap.Pins.Add(examplePinMedical);
customMap.Pins.Add(examplePinShelter);
customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(42.025250, -93.650870), Distance.FromMiles(1.0)));
var addPin = new ToggleButton { Text = "Add pin" };
var buttons = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Children = {
addPin
}
};
Content = new StackLayout
{
Spacing = 0,
Children = {
customMap,
buttons
}
};
}
}
}
我正在尝试做的事情:我正在尝试点击“添加图钉”切换按钮,然后地图将允许我通过点击仅在地图上添加一个图钉,然后是不在另一个图钉上的任何其他连续点击不会在地图上添加另一个图钉。