首页 > 代码库 > iOS 地图移动中心点获取

iOS 地图移动中心点获取


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

}

 



//移除地图内全部的大头针

[mapView removeOverlays:mapView.overlays];

[mapView removeAnnotations:mapView.annotations];



iOS 地图移动中心点获取