how can i get the location of a user from the iphone? - iphone

this is not a coding question, its just the concept im trying to achieve(alogirthm) if you like.
basically what i want to do is check if a user is in a particular place i.e the british meseum!! and then sending them a message saying welcome to the british meseum.
i only have three location in my DB
the british meuseum
the madamme tussaud
westminster abbey
i know that i need to use real time checking! so i was thinking that i get the users location, check to see if it matches any three values, and then send message. can you suggest anything better? thanks

Yea, that sounds like it would work. Just be sure to check the location matches within radius (the location will never be exactly the same) i.e.
if ( dist ( userCoordinate - museumCoordinate) < THRESHOLD ) ...
(use the distanceFromLocation: method To calculate this)
Also, to get the real time checking, you might want to use the delegate callback of the CLLocationManager to receive updates when the users location changes.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
// The location has changed to check if they are in one of your places
}

What you want to is called geocoding. Typically is done by sending a request to a service which actually does the conversion for you. Google and some other companies offer this service.
Below can be used as code reference
-(CLLocationCoordinate2D) addressLocation:(NSString *)input {
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[input stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}

Related

Cannot get GPS location on iPhone

My app runs perfectly on the simulator and everything works fine. But now i wanted to thest it on my iPhone and i found out that the GPS funcution don't work.
Here is my code.
[super viewDidLoad];
//eigener Standort
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
eigLat = newLocation.coordinate.latitude;
eigLon = newLocation.coordinate.longitude;
eigLatString = [NSString stringWithFormat:#"%f", eigLat];
eigLonString = [NSString stringWithFormat:#"%f", eigLon];
}
And till now everything is fine. If i use NSLog i get the right coordinates.
But then i want to use my coordinates here:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
news = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:NULL];
for (x=0; x<[news count]; x++)
{
//Berechnung der Entfernung
erdradius = 6371.1;
//Koordinaten von Datenbank in String schreiben
NSString *latitude = [[news objectAtIndex:x] objectForKey:#"Latitude"];
NSString *longitude = [[news objectAtIndex:x] objectForKey:#"Longitude"];
entfernung = 0;
double lat1 = eigLat; //eigener Standort Latitude
double long1 = eigLon; //eigener Standort Longitude
double lat2 = [latitude doubleValue]; //Standort Heurigen Latitude
double long2 = [longitude doubleValue]; //Standort Heurigen Longitude
And there I get everytime 0 for lat1 and lat2. But only if I Run the App on the iPhone. On the Simulator it works fine.
Does someone know why this could be?
The problem is that you did not get yet a GPS location in real world, so
your method connectionDidFinishLoading() is called before didUpdateToLocation() was called.
Therefoe eighValue is still 0.
Check first for valid lat and longitude:
if both are 0 then you did not get a location.
I think that you must change the logic of your program code,
you have to either
1) wait till you have a location, and then start the
connection such that connectionDidFinishLoading is called after you have a GPS coordinate.
Or
2) you store the coordinate result of the network connection, and calculate when you got your first coordinate

Add annotations to a MapView in a loop

I am trying to add some annotations to my mkMapView in this loop:
for (PFObject *fetchedCompany in companyArray)
{
Store *store = [[Store alloc] initWithObject:fetchedCompany];
[self loadStore:store];
[store release];
}
- (void) loadStore:(Store *) store {
CLLocationCoordinate2D location = [self getLocationFromAddressString:store.address];
MapViewAnnotation *mapAnnotation = [[MapViewAnnotation alloc] initWithTitle:store.name coordinate:location];
[self.indexMapView addAnnotation:mapAnnotation];
}
And this the getLocationFromAddressString method that convert the address to a location using google api:
-(CLLocationCoordinate2D) getLocationFromAddressString:(NSString*) addressStr {
NSString *urlStr = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", [addressStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSError *error = nil;
NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF8StringEncoding error:&error];
NSArray *items = [locationStr componentsSeparatedByString:#","];
double lat = 0.0;
double lon = 0.0;
if([items count] >= 4 && [[items objectAtIndex:0] isEqualToString:#"200"]) {
lat = [[items objectAtIndex:2] doubleValue];
lon = [[items objectAtIndex:3] doubleValue];
}
else {
NSLog(#"Address, %# not found: Error %#",addressStr, [items objectAtIndex:0]);
}
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;
return location;
}
When I send just one location always it works perfectly, but when I add annotations in my loop sometimes some locations could not be recognized and I got the error from my NSLog:
Address, Vallgatan 3 GĂ–TEBORG not found: Error 620
The thing is I am pretty sure about the addresses because when I tried them without loop they worked. Do you know how can I solve the problem?
You've submitted too many queries and your request got rejected. See Geocoding API V2 docs.
620: G_GEO_TOO_MANY_QUERIES
"The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly"

how to get current position?

I have been provided with this address and told to find the current location of me
http://maps.google.com/maps/geo?q=address&output=csv
what kind of address is this and how can i get the information about this . Is this API ?
Please help
suppose if you give http://maps.google.com/maps/geo?q=newyork&output=csv, the result will be
200,4,40.7143528,-74.0059731.The last 2 values mean latitude and longitude value for New york...I hope it will help you.If you integrate this url in your app, you can get co-ordinates and use them to show the map in MKMapView in iPhone.
602,0,0,0
this is your location coordinates. and according to Google map you are located here
With that you can get latitude and longitude of an address. If you request this url and you use the address you want to find the coordinates you get 4 value as a response. You can use this function to get a Location object with lat and log of your address:
-(CLLocationCoordinate2D) addressLocation:(NSString *)address {
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",
[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
NSArray *listItems = [locationString componentsSeparatedByString:#","];
double latitude = 0.0;
double longitude = 0.0;
if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:#"200"]) {
latitude = [[listItems objectAtIndex:2] doubleValue];
longitude = [[listItems objectAtIndex:3] doubleValue];
}
else {
//Show error
}
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
return location;
}
You can find more help here: http://mithin.in/2009/06/22/using-iphone-sdk-mapkit-framework-a-tutorial

how to find latitude and longitude of different locatoins in iphone

I am a new iphone developer,please help me to find out latitude and longitude of different locations(present location values, distance 10m form present locatoin)and find address of that locations using that latitude and longituse values.
using mapkit framework you can find the current location along with near by location with their latitude & longitude
CLLocationCoordinate2D location;
location = [mMapView.userLocation coordinate];
if(iLat && iLng) {
location.latitude = [iLat floatValue];
location.longitude = [iLng floatValue];
}
In order to find out lat & long use this link with complete instruction & sample
http://www.switchonthecode.com/tutorials/getting-your-location-in-an-iphone-application
And in order to get location address using latitude & longitude this can be done by MKReverseGeoCoder.... get in detail using following link
http://blog.objectgraph.com/index.php/2009/04/03/iphone-sdk-30-playing-with-map-kit-part-2/
If your new new to Iphone development, you might wanna return that phone and start developing on Android!
Na just kidding, this will work indeed:
CLLocationCoordinate2D location; location = [mMapView.userLocation coordinate];
if(iLat && iLng) {
location.latitude = [iLat floatValue];
location.longitude = [iLng floatValue];
}
What you want to do is called reverse geocoding. On iOS, this is easily done using MapKit's MKReverseGeocoder.
Here's what you need to do:
First, check if location services is enabled:
if ([CLLocationManager locationServicesEnabled])
[self findUserLocation];
Then in your findUserLocation method, instantiate your CLLocationManager and start receiving updates:
- (void)findUserLocation
{
CLLocationManager *locationManager_ = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracy{ChooseYourOptionHere};
[locationManager startUpdatingLocation];
}
You then implement this delegate method, which will get called every time an update is received. Check the current location's accuracy against your desired accuracy (set above), center the map if you are happy with it AND don't forget to stop the location manager from receiving updates:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if ([newLocation horizontalAccuracy] < [manager desiredAccuracy])
{
// Accuracy is good enough, let's reverse geocode it
[manager stopUpdatingLocation];
[self reverseGeocodeWithCoordinate:newLocation.coordinate];
}
// else keep trying...
}
Once you're happy with the location accuracy, you can start the reverse geocoding process:
- (void)reverseGeocodeWithCoordinate:(CLLocationCoordinate*)coordinate
{
MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:coordinate];
geocoder.delegate = self;
[geocoder start];
}
And you will receive the response via delegate methods:
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
geocoder.delegate = nil;
[geocoder autorelease];
// Reverse geocode failed, do something
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
geocoder.delegate = nil;
[geocoder autorelease];
// Reverse geocode worked, do something...
}

how to convert the nsstring value of text into CLLocationCoordinate2D in objective-c for google map

i want to give option to user to put the location name what he want to search in google map from iphone. when user put the location name that particular map should come.
for now i am doing this by fixing the value of the of coordiante in its object latitude and longitude.
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=19.14;
location.longitude=73.10;
is there any way to give the value of this coordinate in text and convert this to the value of CLLocationCoordinate2D ?
I am not entirely clear on your question, but if you are wanting to convert an NSString into a CLLocationCoordinate2D you can use the following:
{
[self useLocationString:#"19.14,73.10"];
}
- (void) useLocationString:(NSString*)loc
{
// the location object that we want to initialize based on the string
CLLocationCoordinate2D location;
// split the string by comma
NSArray * locationArray = [loc componentsSeparatedByString: #","];
// set our latitude and longitude based on the two chunks in the string
location.latitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:0] doubleValue]] autorelease];
location.longitude = [[[NSNumber alloc] initWithDouble:[[locationArray objectAtIndex:1] doubleValue]] autorelease];
// do something with the location
}
This code doesn't check the validity of the string, which you could do be checking the NSArray you get back from componentsSeparatedByString.
Here's a more modern approach.
- (CLLocationCoordinate2D) get2DCoordFromString:(NSString*)coordString
{
CLLocationCoordinate2D location;
NSArray *coordArray = [coordString componentsSeparatedByString: #","];
location.latitude = ((NSNumber *)coordArray[0]).doubleValue;
location.longitude = ((NSNumber *)coordArray[1]).doubleValue;
return location;
}