views:

4502

answers:

7

I'm trying to get the users current latitude and longitude with this viewDidLoad method. The resulting map is correctly indicating the current location however the NSLog consistently shows:

2009-09-19 16:45:29.765 Mapper[671:207] user latitude = 0.000000
2009-09-19 16:45:29.772 Mapper[671:207] user longitude = 0.000000

Anyone know what I am missing here? Thanks in advance for your help!

- (void)viewDidLoad {
    [super viewDidLoad];
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
    [mapView setShowsUserLocation:YES];

    CLLocation *userLoc = mapView.userLocation.location;
    CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

     NSLog(@"user latitude = %f",userCoordinate.latitude);
     NSLog(@"user longitude = %f",userCoordinate.longitude);






}
+2  A: 

From the document on CLLocation, if the horizontal accuracy or vertical accuracy is negative, it means it can't find a value. I suspect that's what is happening here. Bear in mind that if you just look once, it may give you an approximate last known location before ever improving the accuracy result.

Horizontal Accuracy The coordinate’s latitude and longitude identify the center of the circle and this value indicates the radius of that circle. A negative value indicates that the coordinate’s latitude and longitude are invalid.

AlBlue
so then what is the way to get the coordinates of the current location?
ennuikiller
If you just want to find the current location, you don't need to use the Maps API at all. You can query Core Location directly.http://developer.apple.com/mac/library/documentation/CoreLocation/Reference/CLLocationManager_Class/CLLocationManager/CLLocationManager.html
AlBlue
+3  A: 

The map will not try to get the users location until it's actually displayed, as far as I know. A good test of that is, when do you get the dialog asking you if it's OK for the app to get the users location? In my experience that has always come up only after the map shows on screen.

The best way to approach this problem if you must have a location on startup, is to implement the classic Core Location handler code and get the location from that initially. You can stop receiving updates from there once the map is up and get further changes from there (though if you need constant updates you are better off just keeping with the standard Core Location updates. If you think about it, when you have "shows user location" enabled in the map further use of Core Location by your app is essentially free since the GPS will be fired up the whole time anyway.

Kendall Helmstetter Gelner
+1  A: 

You should get the coordinate from CoreLocation instead of MapKit. MapView will display the user's location after it actually got current location. Before the location is determined, the latitude and longitude are both set to 0. 3

You should create a CLLocationManager instace, set delegate. You will be notified when the location is determined.

Mike Chen
+1  A: 

If you try to get user co-ordinates in viewDidLoad() method, it will always give 0,0 result, because it is not yet initialized.

You should take a look at "Where Am I" sample code form Apple site. It explains what you need. You need to use CLLocationManager class. You will also need the method

*-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation )oldLocation

This method will be called automatically every time when user location is updated. If you are using simulator, then this method will be called only once and it will return some default co-ordinates.

Hope it helps.

Napster
A: 

can you please give me the link for sample code for locating the present location of user. thanks in advance

fgt
A: 

This answer explains how to set a map view with a user location:

http://stackoverflow.com/questions/1437568/howto-initialise-mkmapview-with-a-given-user-location

petert
A: 

In your viewDidLoad section put this...

mapView.showsUserLocation=TRUE;

it should work... it did for me...

theMik