How to track the click on a part of the pin in google maps?

DenisShevchenkoDenisShevchenko USMember ✭✭
edited September 12 in Xamarin.Forms

Hello, I am using Xamarin.Forms.GoogleMaps.Map. To customize the display of the pin, I assign BitmapDescriptor to the icon property. Example:

pin.Icon = BitmapDescriptorFactory.FromView(new AdressPinWithTextPlus());

AdressPinWithTextPlus is a ContentView, so I tried to configure Binding for individual elements in the view. However, my AdressPinWithTextPlus is converted to Bitmap, so I can not trace the events of clicking on the individual elements of the view (example on the attached image to the question).
What are the methods for solving this problem?

Best Answer

  • DenisShevchenkoDenisShevchenko US ✭✭
    Accepted Answer

    The solution was to place the map in RelativeLayout and separately add pins in the form of View in RelativeLayout. Next, bind events to these Views and, when the map moves, move pins using TranslateTo on RelativeLayout. To move, latitude and longitude are converted to pixels

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited September 12

    Inside class AdressPinWithTextPlus

        public event EventHandler Event1;
        public event EventHandler Event2;
        private void Button_Clicked(object sender, EventArgs e)
        {
            Event1(sender, e);
        }
    
        private void Button_Clicked_1(object sender, EventArgs e)
        {
            Event1(sender, e);
        }
    

    handle event

       AdressPinWithTextPlus p = new AdressPinWithTextPlus();
       p.Event1 += (sender,e) =>{ };
       p.Event2 += (sender,e) =>{ };
       pin.Icon = BitmapDescriptorFactory.FromView(p);
    
  • DenisShevchenkoDenisShevchenko USMember ✭✭

    @ColeX said:
    Inside class AdressPinWithTextPlus

        public event EventHandler Event1;
        public event EventHandler Event2;
        private void Button_Clicked(object sender, EventArgs e)
        {
            Event1(sender, e);
        }
    
        private void Button_Clicked_1(object sender, EventArgs e)
        {
            Event1(sender, e);
        }
    

    handle event

       AdressPinWithTextPlus p = new AdressPinWithTextPlus();
       p.Event1 += (sender,e) =>{ };
       p.Event2 += (sender,e) =>{ };
       pin.Icon = BitmapDescriptorFactory.FromView(p);
    

    This does not work because BitmapDescriptorFactory.FromView (p) returns a BitmapDescriptor and this is a bitmap, and the object "p" I think is deleted by the garbage collector

  • DenisShevchenkoDenisShevchenko USMember ✭✭

    I was thinking of making a group of three pins:
    1. For a circle
    2. For button 1
    3. For button 2
    However, the problem is that I can’t place them fixedly because I place them using coordinates (Lat, Lng) and when scaling the map they will visually move to each other or from each other

  • ColeXColeX Member, Xamarin Team Xamurai
    edited September 16

    I think you could get the click event in custom renderer .

    On iOS , customize the Annotation .

    On Android , customize the info window

  • DenisShevchenkoDenisShevchenko USMember ✭✭

    @ColeX said:
    I think you could get the click event in custom renderer .

    On iOS , customize the Annotation .

    On Android , customize the info window

    This solution does not fit because even annotation is converted to a bitmap
    Note from the documentation at this link https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/map/customized-pin

    An info window is not a live View. Instead, Android will convert the View to a static bitmap and display that as an image. This means that while an info window can respond to a click event, it cannot respond to any touch events or gestures, and the individual controls in the info window cannot respond to their own click events.

  • DenisShevchenkoDenisShevchenko USMember ✭✭
    Accepted Answer

    The solution was to place the map in RelativeLayout and separately add pins in the form of View in RelativeLayout. Next, bind events to these Views and, when the map moves, move pins using TranslateTo on RelativeLayout. To move, latitude and longitude are converted to pixels

Sign In or Register to comment.