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