如何获取 MKMapView 地图的中心?

发布于 2024-09-08 02:25:26 字数 919 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2024-09-15 02:25:26

即,

CLLocationCoordinate2D centre = [locationMap centerCoordinate];

如果 centerCoordindate 属性全为 0,请检查您是否拥有有效的 locationMap 指针 - Objective-C 将允许您向 nil 发送消息而不会出现任何错误!

尝试 NSLog(@"%@", locationMap); - 如果输出为零,您可能忘记将 mapLocation 连接到 Interface Builder 中的 MKMapView ;)

What about the centerCoordinate property?

i.e.

CLLocationCoordinate2D centre = [locationMap centerCoordinate];

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文