如何获取 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;
目前正在编译,没有任何警告或错误。开始怀疑我是否错过了一些明显的事情?
I have an MKMapView which allows the user to scroll the map around. Later, I want to get the latitude and longitude of the point at the center of the map, but can't find an easy way to do it. At the moment I'm trying something like:
CLLocationCoordinate2D centre = [locationMap convertPoint:locationMap.center toCoordinateFromView:locationMap];
txtLatitude.text = [NSString stringWithFormat:@"%f",centre.latitude];
txtLongitude.text = [NSString stringWithFormat:@"%f",centre.longitude];
But it's not working - both latitude and longitude both come out as zero. I'd be grateful for any ideas anyone might have!
==============
-- Update 1 --
Oh. If I add the:
NSLog(@"%@", locationMap);
line as suggested below, the log shows "(null)". I've got the following in my header (amongst other things):
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
IBOutlet MKMapView *locationMap;
@property (nonatomic, retain) IBOutlet MKMapView *locationMap;
and the following in my methods file:
@synthesize locationMap;
This is compiling without any warnings or errors at present. Starting to wonder if I've missed something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
即,
如果
centerCoordindate
属性全为 0,请检查您是否拥有有效的 locationMap 指针 - Objective-C 将允许您向 nil 发送消息而不会出现任何错误!尝试
NSLog(@"%@", locationMap);
- 如果输出为零,您可能忘记将 mapLocation 连接到 Interface Builder 中的 MKMapView ;)What about the centerCoordinate property?
i.e.
If the
centerCoordindate
property is all 0, check that you've got a valid locationMap pointer - objective-c will let you send messages to nil without any errors!Try
NSLog(@"%@", locationMap);
- if that outputs nil, you've probably forgotten to connect the mapLocation to a MKMapView in Interface Builder ;)