I am using MKMapView and it is showing my current location. in india it is showing well and exact what in google map but in US current location is showing wrong.
i am using
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
any help please ?
If you are using an MKMapView you can use its delegate method mapView:didUpdateUserLocation:
You need to set showsUserLocation property of your map view to YES.
Then the map view uses CoreLocation framework to determine current location.
It tracks userLocation by itself whenever it receives location updates and updates it periodically.
Related
There is a CLLocationManager i have created, and receiving the gps updates from that , but my doubt is whether the mapview has its own locationmanager, in that case there will be two location managers right? Whether this will affect my application? in case its having its own location manager
MKMapView has its own location manager.That's why you can directly show user location on map like this myMapView.showsUserLocation=YES;
Why are you using separate location manager im MKMapView? You can capture current user location by this way.
CLLocationCoordinate2D location = [[myMapView userLocation] location].coordinate;
double currentLat = location.latitude;
double currentLong = location.longitude;
Also I dont think using separate location manager in MKMapView cause any problems.
Yes, it does. And it shouldn't effect your application as long as you choose one and stick with it. Although, I will point out that having two (or one empty) is terrible coding practice. Use the following function to access your MKMapView's locationManager:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
I have a mapView with custom annotations and just ran into a problem on a beta tester's iPhone. The mapView won't let the user move to any location.. as soon as you try to move, it snaps right back to the original coordinates.
Any idea why? It doesn't happen in the simulator, and I notice it a little bit on my own device... but it is a consistent problem on another device.
Thanks so much!
static BOOL haveAlreadyReceivedCoordinates = NO;
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if(haveAlreadyReceivedCoordinates) {
return;
}
haveAlreadyReceivedCoordinates = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
[mapView setCenterCoordinate:loc];
}
Icky put you on the right track. You shouldn't be constantly resetting the map's center to whatever you get back from the location manager. The location manager will send you all sorts of updates.
You may wish to only use the update once and then ignore future updates (by setting some kind of flag), or you may wish to center the map on the user's location only when the user presses a button. In the latter case the only thing you need to do in didUpdateToLocation: is store the new location into some member variable.
The reason you don't see this in the simulator is that the location hardware simulator isn't constantly updating the way the real one is.
I need to display horizontalAccuracy of currentLocation on the map in my app. To do that I put a line of code in viewDidLoad();
(_currentLocation is CLLocationManager's location.)
label4Accuracy.text = [NSString stringWithFormat:#"Accuracy: %.fm", _currentLocation.horizontalAccuracy];
And it display's accuracy.
The problem is the accuracy does not change. The horizontalAccuracy changes its accuracy if we wait enough (not always though).
What I want my app to be able to do is that it changes the accuracy and display it on the map in real time.
I expected there might be a method somewhere near CLLocationManager like;
- (CLLocationAccuracy *)horizontalAccuracyUpdated():
but there isn't. So my question is where should I put the line of code above to display the horizontalAccuracy of currentLocation that changes in accordance to the actual device's current accuracy?
It's unclear from your question whether you're using your own CLLocationManager instance, or getting current location from an MKMapView.
If you're just using a MKMapView, your map view is going to make a delegate call to the method - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation every time it updates the user's location, and that userLocation variable it passes will have a .horizontalAccuracy parameter that gives the accuracy in meters.
So set your view controller as the map view's delegate and implement that method and watch there for updates.
If you're actually using a CLLocationManager, the same is true except the delegate method is - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation.
I am using
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
to keep receiving location information, then add annotation to MKMapView. It appears that it stop updating when iPhone screen in screen saving mode.
Could you turn off the screensaving mode? You can do so by calling: [UIApplication sharedApplication].idleTimerDisabled = YES; This should solve your problem.
I'm trying to implement Application Tests as described here. So far, so good, but i fail to test, for instance, the location of the device using Core Location. I have added the appropriate Framework to the Target, and have initiated the update of location, but i have no clue of how to wait for the location to be loaded, the test suite just ends before the second thread finish. Please help me to find a way to test this sort of things.
If you're relying on CLLocationManager you can implement these two delegate methods in CLLocationManagerDelegate:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
Then you set the CLLocationmanager delegate and tell it to startUpdatingLocation. If you get didUpdateLocation (with oldLocation set to nil) it means you now have a location and you can consider the test a success (make sure you turn off updating). If you get the other one there was a location-manager error.
If you're relying on MapKit and are using MKMapView's user-location update mechanism, then take a look at my response to this question and implement the observer section (observeValueForKeyPath) to be notified when the map has a location (success) or implement MKMapViewDelegate's mapViewDidFailLoadingMap to be notified if there was an error.