Pxaml
Pxaml

Reputation: 555

How to obtain the coordinates when a pin drag from point a to b using custom map render ios

I would like to translate this OBJC snippet into Xamarin forms custom render, my syntax is currently not working. It crashes during the pin drag process without any error.

what I currently have is

protected override void OnElementChanged(ElementChangedEventArgs<View> e)
        {
            base.OnElementChanged(e);

            if (e.OldElement != null)
            {
                var nativeMap = Control as MKMapView;
                nativeMap.ChangedDragState-= OnDragState;

            }

            if (e.NewElement != null)
            {
                var formsMap = (CustomMap)e.NewElement;
                var nativeMap = Control as MKMapView;
                nativeMap.ChangedDragState+= OnDragState;

            }
        }

        private void OnDragState(object sender, MKMapViewDragStateEventArgs e)
        {
         var NewState = e.NewState;
         var OldState=e.OldState;
         var ShowAnnotation=e.AnnotationView;

         if (OldState==MKAnnotationViewDragState.Dragging) 
         {
         }
         if (NewState == MKAnnotationViewDragState.Ending)
            {
              CLLocationCoordinate2D pinAt = ShowAnnotation.Annotation.Coordinate;
              var droppedLoc = new CLLocation(pinAt.Latitude, pinAt.Longitude);
              geocodeLocation(droppedLoc);


         }
      }

Upvotes: 0

Views: 143

Answers (1)

SushiHangover
SushiHangover

Reputation: 74174

Based upon the ObjC that you have shown:

public override void ChangedDragState(MKMapView mapView, MKAnnotationView annotationView, MKAnnotationViewDragState newState, MKAnnotationViewDragState oldState)
{
    if (oldState == MKAnnotationViewDragState.Dragging)
    {

    }
    if (newState == MKAnnotationViewDragState.Ending)
    {
        var pindropped = annotationView.Annotation.Coordinate;
        var droppedLoc = new CLLocation(pindropped.Latitude, pindropped.Longitude);
        geocodeLocation(droppedLoc);
        mapView.AddAnnotation(_addressAnnotation);
    }
}

Note: You have not shown the ObjC for geocodeLocation and _addressAnnotation so you will need to translate those also...

Upvotes: 1

Related Questions