使用自定义地图渲染iOS如何在大头针从点a拖动到点b时获取坐标

时间:2019-04-17 16:14:38

标签: forms xamarin maps mkannotationview

我想将此OBJC代码片段转换为Xamarin表单自定义呈现,但我的语法目前无法正常工作。在销拖动过程中崩溃,没有任何错误。

我目前拥有的是

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);


         }
      }

1 个答案:

答案 0 :(得分:1)

基于您显示的ObjC:

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);
    }
}

注意:您尚未显示geocodeLocation_addressAnnotation的ObjC,因此您还需要翻译它们...

相关问题