views:

57

answers:

1

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?

+1  A: 

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

deanWombourne
I've tried that too - returns zero as well.
Haydn
Is locationMap a valid pointer - what does __NSLog(@"%@", locationMap);__ say?
deanWombourne
Thanks - the log gives "(null)" - see update above.
Haydn
Have you connected your locationMap in interface builder - it won't automatically know that you want the map in your xib to be connected to the locationMap property!
deanWombourne
I had indeed completely forgotten to do that! Told you it would be something obvious - thanks!
Haydn