2010-07-01 81 views
12

我有一个MKMapView,它允许用户滚动地图。后来,我想获得地图中心的纬度和经度,但找不到一个简单的方法。目前我正在尝试这样的:如何获取MKMapView地图的中心?

CLLocationCoordinate2D centre = [locationMap convertPoint:locationMap.center toCoordinateFromView:locationMap]; 
txtLatitude.text = [NSString stringWithFormat:@"%f",centre.latitude]; 
txtLongitude.text = [NSString stringWithFormat:@"%f",centre.longitude]; 

但它不工作 - 经度和纬度都出来为零。我会很感激任何人可能有的想法!

==============

- 更新1 -

哦。如果我添加:

NSLog(@"%@", locationMap); 

如下建议的行,日志显示“(null)”。我有我的头以下(除其他事项外):

#import <UIKit/UIKit.h> 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 

IBOutlet MKMapView *locationMap; 

@property (nonatomic, retain) IBOutlet MKMapView *locationMap; 

,并在我的方法文件如下:

@synthesize locationMap; 

这是没有编制,目前任何警告或错误。开始怀疑我是否漏掉了一些明显的东西?

回答

38

那么centerCoordinate属性呢?

CLLocationCoordinate2D centre = [locationMap centerCoordinate]; 

如果centerCoordindate酒店是全0,检查是否有一个有效的locationMap指针 - Objective-C的会让你发送消息到零,没有任何错误!

尝试NSLog(@"%@", locationMap); - 如果输出为零,你可能已经忘记了mapLocation连接到界面生成器的MKMapView;)

+0

我已经试过了太 - 返回零。 – Haydn 2010-07-02 08:29:00

+0

locationMap是一个有效的指针 - __NSLog(@“%@”,locationMap); __说什么? – deanWombourne 2010-07-02 08:41:53

+0

谢谢 - 日志给出“(空)” - 见上面的更新。 – Haydn 2010-07-02 17:04:30