how to get current position? - iphone

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

Related

Google maps API error: "stringwithcontentsofurl is deprecated"

I have code that keeps giving me an "stringwithcontentsofurl is deprecated" error and I can't figure out how to fix it. The app keeps taking me to google maps and showing the center of the ocean at 0.0 lat, 0.0 long. This is the code I have now...
NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr]];
NSLog(#"locationstr:%#",locationStr);
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]);
}
NSLog(#"latlonislatlon:%.12f,%.12f,urlstr:%#",lat,lon,urlStr);
CLLocationCoordinate2D location;
location.latitude = lat;
location.longitude = lon;
thanks for the help!
Because your variables have been set to 0.0 for latitude and longitude.
Use this to remove deprecation warning
NSString *locationStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlStr] encoding:NSUTF16BigEndianStringEncoding error:nil];
Also try using different lat, long since their might be problem with your lat long
It has been replaced with
stringWithContentsOfURL:encoding:error:
or
stringWithContentsOfURL:usedEncoding:error:

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"

Registration timer expired, but client is still registering! - Google geocoding error

I am trying to use these two lines to get lat/long info when given an address input.
NSString *urlString = [NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv", theAddress];
NSString *locationString = [[[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:urlString]] autorelease];
It is weird that sometimes I get the lat, long info as quick as in 5 seconds and in other cases the app is stuck for about 5 mins and then it logs an error stating "Registration timer expired, but client is still registering!"
It also gives the location info (ie result string wth lat/long info) as null.
I tried it a number of times and couldn't figure out what makes it work and what doesn't.
Has anybody encountered the same issue before or any advice on this pls.
Thanks
try below code its working fine with me.
CLLocationCoordinate2D coord=[self gettingLatandLonFromAddress:address];
-(CLLocationCoordinate2D ) gettingLatandLonFromAddress:(NSString *)address
{
CLLocationCoordinate2D currentLoc;
NSString *urlString=[NSString stringWithFormat:#"http://maps.google.com/maps/geo?q=%#&output=csv",[address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSASCIIStringEncoding error:nil];
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{
NSLog(#"Error");
}
currentLoc.latitude=latitude;
currentLoc.longitude=longitude;
return currentLoc;
}

how can i get the location of a user from the 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;
}

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