Add annotations to a MapView in a loop - iphone

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"

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:

iPhone SDK: Nearby Locations Feature

I have an iphone app which displays a list of businesses and their location on a Google Map using the MKMapView. As a new feature, I am trying to add a "Nearby" feature, whereby it gets the users current location and displays several map annotations on a google map within the vicinity of say 10KM. Currently, the businesses location is stored as an address string in a mysql database, this is then retrieved and geocoded to display it on a google map.
How do I go about doing this? If there is any tutorials around, please point me to one. (I haven't had any luck with finding one).
Thanks in advance!
UPDATE:
I got it partially working - it's only displaying businesses within a 20KM radius which is great, the problem i'm having is that when they open this Nearby Businesses View, it takes quite some time for it to go through each business and check the distance between the user and each individual business. Is there any way to speed this up? Here is my code:, both these methods are called from the viewDidLoad method.
-(void)getMapData
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSInteger *bizMax = [[numOfBiz objectAtIndex:0]intValue];
int x = 0;
while(x < bizMax)
{
NSURL *mapURL = [NSURL URLWithString:[NSString stringWithFormat:#"getBizMap-xml.php?biz_id=%#",[bizIDS objectAtIndex:x]]];
NSMutableArray *mapArray = [[NSMutableArray alloc] initWithContentsOfURL:mapURL];
self.mapLocation = mapArray;
self.mapStringLocation = [mapArray objectAtIndex:0];
NSLog(#"Location: %#",mapStringLocation);
[mapArray release];
CLLocationCoordinate2D trueLocation = [self getLocationFromAddressString:mapStringLocation];
AddressAnnotation *addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:trueLocation];
CLLocation *bizLoc = [[CLLocation alloc] initWithLatitude:trueLocation.latitude longitude:trueLocation.longitude];
CLLocation *userLocation = [[CLLocation alloc] initWithLatitude:nearbyMapView.userLocation.coordinate.latitude longitude:nearbyMapView.userLocation.coordinate.longitude];
//double distance = [userLocation getDistanceFrom: bizLoc] / 1000;
double kilometers = [userLocation distanceFromLocation:bizLoc] / 1000;
NSLog(#"Distance from %# to current location is %g",mapStringLocation,kilometers);
if(kilometers < maxDistance)
{
[self.nearbyMapView addAnnotation:addAnnotation];
[addAnnotation release];
}
x++;
}
[pool release];
}
-(void)getNumOfBusinesses
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
NSString *numOfBizJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"numOfBiz.php"]]];
NSString *bizIDSJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"allBizIDSJSON.php"]]];
SBJsonParser *parser = [[SBJsonParser alloc]init];
numOfBiz = [[parser objectWithString:numOfBizJSON error:nil]copy];
bizIDS = [[parser objectWithString:bizIDSJSON error:nil]copy];
NSLog(#"biz id 1 = %#",[bizIDS objectAtIndex:0]);
[parser release];
[numOfBizJSON release];
[bizIDSJSON release];
[pool release];
}
This isn't that hard. You can fetch all your data, calculate the distance between you and the location. The next step would be comparing the distance with the maxDistance(the search radius). If the distance < maxDistance, add you can display that location.
I think this could be achieved with very little cocoa knowledge...
If you need something more specific, start coding and I'll help you with that.

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

distance from CurrentLocation to data from Plist File

I have a long list of places with Latitudes and Longitudes in a plist. I want to show a tableView of the places only within X distance of the user's current location. Is there a way to create objects from the lats & longs in the plist file so I can use 'distanceFromLocation'? More importantly, how do I get the array to only display the names with a distance from current less than X? I'm assuming I would need to make a series of objects from lats & longs in the plist, then do an objects in array if objects distanceFrom is less than X, correct?
Please help.
Here's where I am now: I get an error on the double clubLatitude line
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *clubArray = [NSArray arrayWithObjects:[self danceClubLocation], nil];
self.tableData = clubArray;
}
-(CLLocation *)danceClubLocation
{
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];
NSEnumerator *e = [array objectEnumerator];
id object;
while ((object = [e nextObject])) {
double clubLatitude = [[array valueForKey:#"Latitude"] doubleValue];
double clubLongitude = [[array valueForKey:#"Longitude"] doubleValue];
CLLocation *clubLocation = [[CLLocation alloc] initWithLatitude:clubLatitude longitude:clubLongitude];
if ([clubLocation distanceFromLocation:myLocation]<=50) {
return clubLocation;
}
else return nil;
}
return nil;
}
-(CLLocation *)myLocation
{
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
double myLatitudeD = [myLatitude doubleValue];
double myLongitudeD = [myLongitude doubleValue];
myLocation = [[CLLocation alloc]initWithLatitude:myLatitudeD longitude:myLongitudeD];
return myLocation;
}
As #DavidNeiss said, you have to iterate over the list (an NSArray with the plist as source) and it would be something like this:
NSString *plistPath = [[NSBundle mainBundle] pathForResource:#"latlong" ofType:#"plist"];
NSArray *array = [NSArray arrayWithContentsOfFile:plistPath];
NSEnumerator *e = [array objectEnumerator];
id object;
while (object = [e nextObject]) {
// do something with object
}
Then you can do what you want (removing what's far from the user or whatever).
Read in your plist, iterate over it to pull out ones within X distance and populate any array with them that will be the data source for your table view?

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