我如何在 MKmap 视图上拖动我的注释,如 Google Pin Drop 和 Draging:iOS
How Can I Drag My Annotation On MKmap View Like Google Pin Drop And Draging:iOS
我需要一些帮助。我如何在 MKMapview 中将注释(地图图钉)从一个点拖到另一个点。
添加过程在地图上的引脚下拉后一切正常,我想拖动该引脚(注释)并更改该位置,就像 Google 地图一样。
快乐编码
过程是您需要允许图钉可拖动,您将在 MKMapView
的 didChangeDragState
上获得目的地的经纬度。
试试下面的代码。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ident"];
pinView.draggable = YES;
pinView.animatesDrop = YES;
return pinView;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
droppedAt = annotationView.annotation.coordinate;
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
首先允许引脚拖动
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id) annotation
{
pin.draggable = YES;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
[annotationView.annotation setCoordinate:droppedAt];
if(DEBUG_MODE){
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
}
要拖动注释,请将注释的可拖动 属性 设置为是。
在 viewForAnnotation
委托方法中设置注释的可拖动。
使用委托方法 didChangeDragState
方法获取注释的新坐标。
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateStarting) {
}
if (newState == MKAnnotationViewDragStateEnding) //Annotation dragging ended
{
CLLocationCoordinate2D droppedCord = annotationView.annotation.coordinate;
NSLog(@"New position %f,%f", droppedCord.latitude, droppedCord.longitude);
}
}
我需要一些帮助。我如何在 MKMapview 中将注释(地图图钉)从一个点拖到另一个点。 添加过程在地图上的引脚下拉后一切正常,我想拖动该引脚(注释)并更改该位置,就像 Google 地图一样。
快乐编码
过程是您需要允许图钉可拖动,您将在 MKMapView
的 didChangeDragState
上获得目的地的经纬度。
试试下面的代码。
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id < MKAnnotation >)annotation
{
MKPinAnnotationView *pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ident"];
pinView.draggable = YES;
pinView.animatesDrop = YES;
return pinView;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
droppedAt = annotationView.annotation.coordinate;
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
首先允许引脚拖动
- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id) annotation
{
pin.draggable = YES;
}
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateEnding)
{
CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
[annotationView.annotation setCoordinate:droppedAt];
if(DEBUG_MODE){
NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}
}
要拖动注释,请将注释的可拖动 属性 设置为是。
在 viewForAnnotation
委托方法中设置注释的可拖动。
使用委托方法 didChangeDragState
方法获取注释的新坐标。
- (void)mapView:(MKMapView *)mapView
annotationView:(MKAnnotationView *)annotationView
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
if (newState == MKAnnotationViewDragStateStarting) {
}
if (newState == MKAnnotationViewDragStateEnding) //Annotation dragging ended
{
CLLocationCoordinate2D droppedCord = annotationView.annotation.coordinate;
NSLog(@"New position %f,%f", droppedCord.latitude, droppedCord.longitude);
}
}