Does a MKMapView have its own location Manager? - iphone

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;

Related

MKMapview showing current location wrong

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.

iPhone LocationManager - Getting Coordinates at App Start

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

MapView snaps back to original location - help!

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.

Calculate and display distance between userlocation and known point in Table View

I have a table view with a list of hotel, and i want put in cell.detailTextLabel.text the distance beetween userlocation and hotel. How can obtain the coordinates of userlocation?
I see on web that i need to use CLLocationManager but i don't understand how and where implement in my table view.
Then,to get the distance,i do a "getDistancefrom" between userLocation and the coordinates of the hotel ?
Thanks
That's really quite a big question ;)
This tutorial looks pretty good - I'd go through it and see if it helps. If not, come back to stackoverflow and ask more questions.
Well you have the right approach in mind, just need to clear out the details.
The simplest way is to have your table view controller adhere to the CLLocationManagerDelegate protocol. You then setup a location manager and have it start updating the user's location:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
Then you just implement this method from the delegate:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
In there you will be notified whenever a user's location changes. So then you can grab the newLocation longitude and latitude and compute the distance using all your hotels' longitudes and latitudes.
Make sure you import the core location framework to your project as I don't think it's there by default.
Good luck.

iPhone SDK: Track users location using GPS

I have a few questions about CoreLocation and GPS.
First, what method in core location is used to continually get the users current coordinates? And at what interval should these be retrieved?
Second, should these coordinates be pushed into a NSMutableArray each time they are received, so that the array of coordinates will represent the users path?
Thanks, just wanting to get started getting me mind around this.
A very abbreviated version:
First, adopt the <CLLocationManagerDelegate> protocol in your .h, and #import <CoreLocation/CoreLocation.h>.
Then in .m go:
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(#"%f %f ", here.latitude, here.longitude);
}
Your -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation method will get pinged every time Core Location has something to say to you, which should happen every few seconds. those CLLocation objects contain info about accuracy, so you can screen for good points in that method.
Be sure to call [locationManager stopUpdatingLocation] and then [locationManager release] at some point!
Good luck finding yourself!
The best way to is read the CLLocationManager Class Reference, which links to several example projects. The short version:
Set the delegate property to a class that will receive location updates.
Implement the CLLocationManagerDelegate protocol in the delegate.
Call the appopriate methods to start updating location and/or heading.
You are able to define what range is acceptable for accuracy as well as how often you wish to receive automatic updates (based on a distance from last point mechanism). Also you can just turn off the location manager and turn it back on at will thru some use of a timer.
As for saving the locations to build a path, its not that simple. You will continually get GPS locations at first until the desired accuracy is achieved, and for any points in the future you may get more than one that is inaccurate before you get a good location. So building a list of these points will basically just be a list of their path, along with a lot of extra points.
You could solve this by saving only those points that have the accuracy you desire, but its an imperfect world in this respect.
Best case I would suggest you keep two lists, one is the path and the other is a running list of locations where you are comparing until you get a highly accurate location, then putting that on your path list.
Some of the example projects do things along these lines, do check them out.
You will have to do the following:
If device cannot access internet
Get co-ordinates from GPS device
Send these co-ordinates via SMS
Receive and decode SMS message at the SMS gateway you have to configure to receive info from device.
Update the info on the application database or any other store you are using
Update the position on map with latest info
If device can access internet
Get co-ordinates from GPS device
Connect to application server (may be some service) and upload information
Update the info on the application database or any other store you are using
Update the position on map with latest info