Sai Sunkari
Sai Sunkari

Reputation: 179

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. It there any way to not show the info window and the event triggers as soon as I click on the marker?

Thanks

Upvotes: 0

Views: 3399

Answers (2)

chri3g91
chri3g91

Reputation: 1410

Currently with Xamarin.Forms 5, PinClicked event is designated as obsolete. Same goes for Device.OpenUri. One can use pin1.MarkerClicked += Pin_Clicked; instead. You can prevent the Info window from opening by setting the EventArgs's HideInfoWindow property to 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
        }
}

Upvotes: 0

nevermore
nevermore

Reputation: 15786

Use Map_PinClicked to handle the PinClick event, If you set e.Handled = true, then Pin selection doesn't work automatically. 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.

Upvotes: 3

Related Questions