Google maps API error: "stringwithcontentsofurl is deprecated" - iphone

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:

Related

Assigning lat long to CLLocation giving error not accepting any format

I am giving lat long to CLLocation in this way.
CLLocation *loc = [[CLLocation alloc]init];
loc.coordinate.latitude = [sLat floatValue];
loc.coordinate.longitude = [sLng floatValue];
Coming from
NSMutableDictionary *locat = [dictLoc valueForKey:#"location"];
sLat = [locat valueForKey:#"lat"];
sLng = [locat valueForKey:#"lng"];
Showing correct values in console, but kills when allocated to cllocation.
Please guide for the above.
Thanks.
you can get the value in CLLocationCoordinate2D object like bellow...
CLLocationCoordinate2D location;
location.latitude = [sLat doubleValue];
location.longitude = [sLng doubleValue];
OR Also try to retain that string like bellow..
sLat = [locat valueForKey:#"lat"];
[sLat retain];
sLng = [locat valueForKey:#"lng"];
[sLng retain];
and then assign it to CLLocation
hope this helpful to you....

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"

NSString parsing

I need to parse this string into three different components:
Location: 1|#69.83623|#24.432223|#Cupertino, California
The value is stored in one NSString. I need it in three different strings. One string for latitude, one for longitude and one for location.
Any idea how I can do that?
Thanks!
You can use this method to get an array of different components:
NSArray *bits = [locationString componentsSeparatedByString: #"|#"];
Each item in the NSArray will be an NSString.
Try the following
NSString *location = #"1|#69.83623|#24.432223|#Cupertino, California";
NSArray *components = [location componentsSeparatedByString:#"|#"];
NSLog(#"%#",components);
float latitude = [[components objectAtIndex:1] floatValue];
float longitude = [[components objectAtIndex:2] floatValue];
NSString *loc = [components objectAtIndex:3];
NSString *t = #"Location: 1|#69.83623|#24.432223|#Cupertino, California";
NSArray *k = [t componentsSeparatedByString:#"|"];
NSLog(#"components %#", k);

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