Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone current user location coordinates showing as (0,0)

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);
}
like image 960
ennuikiller Avatar asked Sep 19 '09 20:09

ennuikiller


People also ask

How do I find my current GPS coordinates on my iPhone?

You can use the built-in GPS in your iPhone to display your current location – latitude and longitude coordinates. Follow these simple steps to find your current coordinates in seconds. Minutes and degrees: Go to Settings app and make sure the Location Services is ON. Settings -> Location Services -> ON.

How do I find my current coordinates in seconds?

Follow these simple steps to find your current coordinates in seconds. Minutes and degrees: Go to Settings app and make sure the Location Services is ON. Settings -> Location Services -> ON. Expand the Location Services and make sure that the app Compass is turned ON. Go back to your home screen.

How do I find coordinates of a location without an address?

If you’re searching a location without an address, you’ll be able to retrieve the coordinates by right-clicking on the map where it’ll then display the coordinates. This is a nice feature when you’re needing to find the coordinates of a place that isn’t your current location.

What is core location in iOS?

Core Location framework, that you’ve just added, allows you to retrieve the user’s current location in the form of latitude and longitude. It can even give you continuous location update if the user is on the move. Like other libraries in the iOS SDK, Core Location makes use of the delegate pattern.


4 Answers

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.

like image 62
Kendall Helmstetter Gelner Avatar answered Oct 29 '22 01:10

Kendall Helmstetter Gelner


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.

like image 39
Napster Avatar answered Oct 29 '22 01:10

Napster


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.

like image 41
AlBlue Avatar answered Oct 29 '22 00:10

AlBlue


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.

like image 41
Mike Chen Avatar answered Oct 29 '22 00:10

Mike Chen