views:

65

answers:

2

I am just looking at mapKit and decided to make a quick button to display my current location, however when I press the button my latitude/longitude always display as [0.000000] [0.000000].

The mapView is loaded as I can see the map on the simulator before I press the button. Previously I have done this by using coreLocation.framework and using CLLocationManager and asking for the device location that way. I am just curious why this way is not working correctly, would I be better doing this via CLLocationManager?

-(IBAction)findMePressed {
    MKUserLocation *myLocation = [myMapView userLocation];
    CLLocationCoordinate2D coord = [[myLocation location] coordinate];
    [myMapView setCenterCoordinate:coord animated:YES];
    NSLog(@"findMePressed ...[%f][%f]", coord.latitude, coord.longitude);
}

EDIT: Added ...

-(IBAction)findMePressed {
     MKUserLocation *myLocation = [myMapView userLocation];
     CLLocationCoordinate2D coord = [[myLocation location] coordinate];
     MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 350, 350);
     [myMapView setRegion:region animated:YES];
}

-(void)viewDidLoad {
    [super viewDidLoad];
    [myMapView setShowsUserLocation:YES];
}

Gary.

+1  A: 

Hi

Either the userLocation is not visible on the map (see the userLocationVisible property) or there is some problem setting up the myMapView property and it's nil (i.e. not connected in interface builder)

deanWombourne
Hi Dean, userLocationVisible is [0] so I guess that is the problem. myMapView is connected in IB so its correctly pointing to my iVar.
fuzzygoat
The way my map was displaying Cupertino was just off the left of the display. Setting showUserLocation and making a region did what I was expecting, much appreciated.
fuzzygoat
+1  A: 
[...] as I can see the map on the simulator [...]

Test it on the device. By default, on the simulator, the coordinates you get back are Apple's headquarters. Cf. doc.

See this other SO question for workarounds and useful utilities : http://stackoverflow.com/questions/802156/testing-corelocation-on-iphone-simulator

Guillaume
Hi Guillaume, much appreciated.
fuzzygoat