MKMapView學習系列3- 地圖移動中心點獲取


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM