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!
==============
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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With