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.
Related
I have developed both iPhone & iPad application which supports for iOS 5 and iOS 6
in that application i have grabbed user current location using CLLocationManager.
when i want to stop updating receiving GPS coordinates. I have called stopUpdatingLocation
method to stop calling didUpdateToLocation method. It was woking completely fine with iOS 5 and 6 But unfortunately its not working with iOS 7.
Seems that stopUpdatingLocation method is not working .Any particular reason. ??
i had a keep a variable to monitor the life cycle of didUpdateToLocation method and stop executes it.
One thing might be possible is,
Your current location blue dot moving on your map is because you set the MKMapView's showsUserLocation to YES. It will track until you set it to NO.
Second thing,
You might be setting below thing to stop calling that method
locationManager = nil;
That does not stop monitoring, but what it does, not to refer the location manager, So now its not possible to stop monitoring.
Instead add below code & then see what happen
[locationManager stopUpdatingLocation];
locationManager=nil;
Hope it helps..
#tdevoy - Here is sample
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
latitude = location.coordinate.latitude;
longtitude = location.coordinate.longitude;
//stops updating current user location update
[locationManager stopUpdatingLocation];
}
Have a simple app that gets gps location. I would like to get a gps location before the app initialises. It seems for me that GPS location is updated asynchronously and I do get the app screen on the background loading while the question pops up: "allow appliction to use your current location?"
Well, how can I make a callback (sorry, coming from Ruby / JS background) to wait until I get the current location.
The code:
- (void)viewWillAppear:(BOOL)animated
{
// Set the main view to utilize the entire application frame space of the device.
// Change this to suit your view's UI footprint needs in your application.
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
[CLController.locMgr startUpdatingLocation];
self.view.frame = [[UIScreen mainScreen] applicationFrame];
[super viewWillAppear:animated];
}
in CoreLocationController.m
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"Update location");
if([self.delegate conformsToProtocol:#protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
[self.locMgr stopUpdatingLocation];
# => wait initialisation until this point and than continue
You really just need to get your app up and running.
There are two things I would try (simultaneously) to handle the delay:
misdirection! Start with another screen open. Get the user interested in something else. One tap's worth of delay might be all you need to get a fix particularly if this is not a first run. On the first run when you know it will take a little time, run a little tutorial that will occupy the time.
start with the last location - this may not be right for you if inaccurate position information could be harmful but you could store the last location received when the app exits and use it as your current position when you start the app again. Of course you want to indicate that the location is a guess at best so you might want to draw your own position dot with uncertainty circle, indicating that the whole map is the uncertain area - until you get your first GPS fix and then you can begin drawing real position updates.
I think an artificial delay where you do nothing while waiting for the location to update is going to be a worse user experience than accepting that it will take a short time and trying to do as much as you can in that time.
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 want to track a person's movement on mapview in my iphone. I am using mapview which shows user's current location as well as one more pin which denotes another persons location say person A.I want to track person A physical Movement on my iphone mapview.
That means if person A is moves from one location to another,the icon or the pin which denotes person A's location should move from one location to another on the mapview on my iphone.
I want to track person A live movement on mapview.Is it possible?If Yes please explain how?Please help.
This is not hard. I assume that you are using a server which will communicate between the two devices. In this case when the person A moves you can update his location coordinates to your server. You can get his/her location coordinates using the
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
delegate method. At the same time you can get person A's location coordinates from the server and keep updating your mapview. The pin will move. Just set the pin drop animation to NO.
I'm getting the location through - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation. It should work when you move (can't really test it in simulator, at least I don't know how) but when I first load the app, the mapview is focused on the whole world, not just my location. Can I anyhow call the method to change the location on first view?
Here is my current code if it helps:
http://pastebin.com/9Sb2xVmE
http://pastebin.com/rh9WB2ca
If your using mapkit:
Have you tried
setRegion:(MKCoordinateRegion)region animated:(BOOL)animated
Example:
CLLocationCoordinate2D coord = {latitude: 61.2180556, longitude: -149.9002778};
MKCoordinateSpan span = {latitudeDelta: 0.2, longitudeDelta: 0.2};
MKCoordinateRegion region = {coord, span};
[mapView setRegion:region];
this should zoom the mapview to your desired size, while maintaining focus on the center.
If your using CoreLocation:
Heres a checklist of what might have gone wrong:
Did you remember to import
Do you have a location manager (locationManager = [[CLLocationManager alloc] init];)
Is the code mentioned within the delegate of the location manager ?
I advise you to go to this page : Getting location of a iOS device with objective-C and follow the tutorial bit by bit. That should help you out.
Also, heres the mapkit docs
Best of luck with your project :]
we cant able to get the current location by running it in simulator. if you run the application in device it will show the correct location. In simulator it will show the default loaction as califorina(apple head quaters).