MKMap显示地图后,如果用户移动了地图,自己定义的数据就需要刷新了,所以这个时候,中心点的经纬度就比较重要了。
本文演示如何获取经纬度
在MKMapViewDelegate里有个方法
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
这个方法就是在Map移动 后执行,所以我们可以在这里获取移动后地图中心点的经纬度了。
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
MKCoordinateRegion region;
CLLocationCoordinate2D centerCoordinate = mapView.region.center;
region.center= centerCoordinate;
NSLog(@" regionDidChangeAnimated %f,%f",centerCoordinate.latitude, centerCoordinate.longitude);
}
同样有个问题,如何获取用户点击地图某个区域的经纬度呢?
方法如下
在ViewDidLoad里添加tabGuesture
UITapGestureRecognizer *mTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPress:)];
[self.mapView addGestureRecognizer:mTap];
[mTap release];
-(void)tapPress:(UIGestureRecognizer*)gestureRecognizer
{
CGPoint touchPoint = [gestureRecognizer locationInView:self.mapView ];
CLLocationCoordinate2D touchMapCoordinate =
[self.mapView convertPoint:touchPoint toCoordinateFromView:self.mapView ];
NSLog(@"%f %f",touchMapCoordinate.latitude, touchMapCoordinate.longitude);
}