Any idea about add Pins [Xamarin.Forms.Maps]

Hi everybody,

I am developing a system for IOS and in the future it to Android too,so I'm using Xamarin.Forms.

On my Map App I need add pins on map, the pin will be put in a specific local on map.

What best Idea for development this?

Remember, I already have added pins on the map, but all comes from a database with location already determined. At this new pin who will choose the location is the User.

Best Answer

  • TorbenKruseTorbenKruse DE ✭✭✭
    edited November 2015 Accepted Answer

    @TioWidow

    I would not recommend using single tap to add pins to the map, that may interfere with the selection of a pin.

    For example, you can use LongPress instead. To do that add a UILongPressGestureRecognizer to the MKMapView in your MapRenderer.

    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);
    
        if (e.OldElement != null) return;
    
        var map = this.Control as MKMapView;
        if(map == null) return;
    
        map.AddGestureRecognizer(new UILongPressGestureRecognizer(MapLongPress));
    
    }
    
    private void MapLongPress(UILongPressGestureRecognizer recognizer)
    {
        if (recognizer.State != UIGestureRecognizerState.Began) return;
    
        var map = this.Control as MKMapView;
    
        if(map == null) return;
    
        var pixelLocation = recognizer.LocationInView(map);
        var geoCoordinate = map.ConvertPoint(pixelLocation, map);
    
        // Add new Annotation(pin) with the coordinate
    }
    

Answers

  • ChaseFlorellChaseFlorell CAInsider, University mod
    edited November 2015

    Pins are added via Latitude/Longitude coordinates and the documentation is found here.

    If the user needs to choose a location, you're probably going to have to write a custom renderer that exposes the touch event. There will be a callback with the coordinates that you can then set to your ViewModel if you need to store it later.

  • TorbenKruseTorbenKruse DEMember ✭✭✭
    edited November 2015 Accepted Answer

    @TioWidow

    I would not recommend using single tap to add pins to the map, that may interfere with the selection of a pin.

    For example, you can use LongPress instead. To do that add a UILongPressGestureRecognizer to the MKMapView in your MapRenderer.

    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
        base.OnElementChanged(e);
    
        if (e.OldElement != null) return;
    
        var map = this.Control as MKMapView;
        if(map == null) return;
    
        map.AddGestureRecognizer(new UILongPressGestureRecognizer(MapLongPress));
    
    }
    
    private void MapLongPress(UILongPressGestureRecognizer recognizer)
    {
        if (recognizer.State != UIGestureRecognizerState.Began) return;
    
        var map = this.Control as MKMapView;
    
        if(map == null) return;
    
        var pixelLocation = recognizer.LocationInView(map);
        var geoCoordinate = map.ConvertPoint(pixelLocation, map);
    
        // Add new Annotation(pin) with the coordinate
    }
    
  • TioWidowTioWidow BRMember

    @TorbenKruse said:
    @TioWidow

    I would not recommend using single tap to add pins to the map, that may interfere with the selection of a pin.

    For example, you can use LongPress instead. To do that add a UILongPressGestureRecognizer to the MKMapView in your MapRenderer.

    protected override void OnElementChanged(ElementChangedEventArgs<View> e)
    {
      base.OnElementChanged(e);
    
      if (e.OldElement != null) return;
    
      var map = this.Control as MKMapView;
      if(map == null) return;
      
      map.AddGestureRecognizer(new UILongPressGestureRecognizer(MapLongPress));
      
    }
    
    private void MapLongPress(UILongPressGestureRecognizer recognizer)
    {
      if (recognizer.State != UIGestureRecognizerState.Began) return;
    
      var map = this.Control as MKMapView;
      
      if(map == null) return;
      
      var pixelLocation = recognizer.LocationInView(map);
      var geoCoordinate = map.ConvertPoint(pixelLocation, map);
    
      // Add new Annotation(pin) with the coordinate
    }
    

    Thanks for listening

    I will try to use this with the custom renderer because I am using a object Map ("xamarin.forms.maps") in a Page.

    Anyway I believe that your help will be very useful.

Sign In or Register to comment.