开发者

How to get center of an MKMapView map?

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. 开发者_C百科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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜