UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
[self.mapView addGestureRecognizer:mTap];
事件实现如下:
[cpp] view plaincopyprint?
- (void)tapPress:(UIGestureRecognizer*)gestureRecognizer {
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView];//这里touchPoint是点击的某点在地图控件中的位置
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView];//这里touchMapCoordinate就是该点的经纬度了
NSLog(@"touching %f,%f",touchMapCoordinate.latitude,touchMapCoordinate.longitude);
}
// 以下是生成大头针的方法
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
NSString *AnnotationViewID = @"renameMark";
if (newAnnotation == nil) {
newAnnotation = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
// 设置颜色
((BMKPinAnnotationView*)newAnnotation).pinColor = BMKPinAnnotationColorPurple;
// 从天上掉下效果
((BMKPinAnnotationView*)newAnnotation).animatesDrop = YES;
// 设置可拖拽
((BMKPinAnnotationView*)newAnnotation).draggable = YES;
// newAnnotation.frame=CGRectMake(180, 200, 50, 50);
}
newAnnotation.centerOffset = CGPointMake(0, -(newAnnotation.frame.size.height * 0.5));
newAnnotation.annotation = annotation;
[newAnnotation setSelected:YES animated:YES];
return newAnnotation;
}
MKMap显示地图后,如果用户移动了地图,自己定义的数据就需要刷新了,所以这个时候,中心点的经纬度就比较重要了。
本文演示如何获取经纬度
在MKMapViewDelegate里有个方法
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
这个方法就是在Map移动 后执行,所以我们可以在这里获取移动后地图中心点的经纬度了。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
CLLocationCoordinate2D centerCoordinate = mapView.region.center;
region.center= centerCoordinate;
NSLog( @" regionDidChangeAnimated %f,%f ",centerCoordinate.latitude, centerCoordinate.longitude);
}