简体   繁体   English

xamarin 形成地图的标记单击事件

[英]xamarin forms map's marker click event

I have a map with a single pin on it.我有一张地图,上面有一个大头针。 as follows:如下:

var map = new Map()
                {
                    IsShowingUser = true,
                    HeightRequest = 100,
                    WidthRequest = 960,
                    VerticalOptions = LayoutOptions.FillAndExpand
                };

and the pin location and label as follows:以及引脚位置和标签如下:

var pin1 = new Pin();
pin1.Type = PinType.Place;
pin1.Position = position;
pin1.Label = "Ticket Number: " + Cache.Instance.Ticket.TicketNumber;

clicked event:点击事件:

pin1.Clicked += delegate
{
    uri = new Uri("http://maps.google.com/maps?daddr=" + position.Latitude + "," + position.Longitude);
    Device.OpenUri(uri);
}

map loading:地图加载:

var stack = new StackLayout { Spacing = 00 };
        stack.Children.Add(map); 
        Content = stack;

when clicking on the pin marker, it opens an info window and clicking on the window and clicked event code triggers.单击 pin 标记时,它会打开一个信息窗口并单击该窗口并单击事件代码触发器。 It there any way to not show the info window and the event triggers as soon as I click on the marker?有什么办法可以不显示信息窗口,并且只要我点击标记就会触发事件?

Thanks谢谢

Use Map_PinClicked to handle the PinClick event, If you set e.Handled = true , then Pin selection doesn't work automatically.使用Map_PinClicked处理PinClick事件,如果设置e.Handled = true ,则 Pin 选择不会自动工作。 All pin selection operations are delegated to you.所有引脚选择操作都委托给您。

In the Page:在页面中:

    map.PinClicked += Map_PinClicked;

    // Selected Pin changed
    map.SelectedPinChanged += SelectedPin_Changed;

    map.InfoWindowClicked += InfoWindow_Clicked;

    map.InfoWindowLongClicked += InfoWindow_LongClicked;

And then clickEvent:然后点击事件:

    void Map_PinClicked(object sender, PinClickedEventArgs e)
    {
        e.Handled = true;

        uri = new Uri("http://maps.google.com/maps?daddr=" + position.Latitude + "," + position.Longitude);
        Device.OpenUri(uri);

    }

You can have a look at here for more information.您可以查看此处了解更多信息。

Currently with Xamarin.Forms 5, PinClicked event is designated as obsolete.目前在 Xamarin.Forms 5 中,PinClicked 事件被指定为已过时。 Same goes for Device.OpenUri. Device.OpenUri 也是如此。 One can use pin1.MarkerClicked += Pin_Clicked;可以使用pin1.MarkerClicked += Pin_Clicked; instead.反而。 You can prevent the Info window from opening by setting the EventArgs's HideInfoWindow property to true.您可以通过将 EventArgs 的HideInfoWindow属性设置为 true 来阻止信息窗口打开。 docs.microsoft 微软文档

 private async void Pin_Clicked(object sender, PinClickedEventArgs e)
 {
        try
        {
            e.HideInfoWindow = true;
            var pin = sender as Pin;
            var uri = new Uri("http://maps.google.com/maps?daddr=" + pin.Position.Latitude + "," + pin.Position.Longitude);
            Launcher.OpenAsync(uri);
        }
        catch (Exception ex)
        {
           //log error
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM