views:

960

answers:

1

I know Core Location isnt always accurate on the iPhone. Is there a way to use the location (lat/long) along with the accuracy to then grab all of the possible suburbs/streets in that area?

So instead of using the reverse geocoding and having it guess the 'best possible suburb/street, is there a way to make it give an array (or in some other format) of all the possible suburbs/streets that it could consider?

Thanks!!

+1  A: 

Hey Zac,

So, the best approach to do this, is using the Google maps API. For example, take a look ate the following url: http://maps.google.com/maps/geo?q=38.4417077,-90.7122047&output=xml

As you can see, for this pair (latitude, longitude) you have 10 possible placemarks for this coordinates.

The documentation for this you can find at: http://code.google.com/apis/maps/documentation/geocoding/index.html

You can see that you get the info in XML, JSON, CSV.

Take a look at the status code, it is important to verify if the request was successful (usually 200 or 200 and something).

So, how to use it VFN?

It is simple.



double latitude = coordinate.latitude;
    double longitude = coordinate.longitude;

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f&output=xml", latitude, longitude];   
    NSURL *url = [NSURL URLWithString:urlString];


    NSString *locationString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

on locationString is the response from Google Geocoder, now you can use the info as you want. Remember to first check the status code, and than parse the info. If you want only the first placemark that is selected by Google Geocoder, I would suggest you to use the output in CSV, that is easy to work with.

If you have any queries, let me know!

Cheers,
VFN

vfn
I have no trouble getting the street/suburb/city/postal code. What I want to do it use the coordinate and the radius that Core Location gives me to then get all of the streets/suburbs within the radius from the coordinate. So lets say I have the coordinate (-33.888068,151.205521) and an accuracy of 3km. I want to find all of the suburbs within 3km of that coordinate. Those being Chippendale, Surry Hills, Redfern, Alexandria, Eveleigh, etc.Thanks for the help, but it gives me the same info that I already have.
Zac Altman
Hey Zac,Because of that I put the link of for the documentation on my answer.Take a look at: http://code.google.com/apis/maps/documentation/geocoding/#ViewportsYou can define the center of the region and the span for the bounding box. The code posted above is only to illustrate in the case that you didn't know how to use the API. Let me know if it is enough. Cheers
vfn
So how do I use that information to then get the suburbs? Preferably in an array. Sorry, i havent used XML before. Im using this: [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%f,%f
Zac Altman
Here it is how to work with XML (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html) and you can check the Apple examples at: http://developer.apple.com/mac/library/samplecode/ImageMap/index.html#//apple_ref/doc/uid/DTS40009015
vfn
ok, so i have been able to get it to save an array of the locations. so that is all good, but while it is parsing the xml, the whole UITableView is unusable. So I either need to do some sort of threading or something so that the user can still use the app while the location is being found. Is there a shorthand way with XML, or do I have to get in to threading?
Zac Altman
Usually to do this kind of thing, you create a UIView with a activity indicator, and when your data is all parsed you reload you UITableView. On this link (http://developer.apple.com/iPhone/library/samplecode/TopSongs/index.html) you can download a good example on how to do this. It also uses CoreData to store the information from the XML. Maybe it can be useful or not.
vfn