An easy one to kick things off.
Need to search for places of interest, restaurants, hotels and display them on a map? Google's Local Search can help you out.
Among Google's AJAX APIs is a RESTful webservice which returns JSON formatted data for use in Flash or other non-javascript environments.
Using this webservice you can fire off a request to Google and get the same results as you would if you searched on Google Maps itself - only in a data format that is useful to you.
The base URL you'll need is:
http://ajax.googleapis.com/ajax/services/search/local?
You'll probably want to add a few parameters onto it too:
- v=1.0 - Specify which version of the API you're attempting to call.
- rsz=8 - Specify how many results you wish to get back (minimum 4, maximum 8 ).
- sll=33.830296,-116.545292 - A comma separated coordinate reference that will form the center point that you're searching from.
- q=restaurants - Your search query. This can be anything you like - perhaps you can take a string value from a search bar?.
You final URL should look something similar to:
http://ajax.googleapis.com/ajax/services/search/local?v=1.0&rsz=8&sll=33.830296,-116.545292&q=restaurants
Here's an example of how I make the request. It's pretty straightforward. I use an MKMapView in my app and use it's centerCoordinate property as the centre of my search. I take the query term from a search bar, remembering to URL encode it properly (spaces become %20, etc).
NSString *locationString = [NSString stringWithFormat:@"%.6f,%.6f", [mapView centerCoordinate].latitude, [mapView centerCoordinate].longitude];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://ajax.googleapis.com/ajax/services/search/local?v=1.0&rsz=8&sll=%@&q=%@", locationString, [[searchBar text] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection connectionWithRequest:request delegate:self];
This will give you a response with data formatted in JSON, which you can parse to access the returned locations. You can use a JSON parser such as my personal choice SBJSON or TouchJSON. A blog post on JSON parsing may come in the future.
The JSON data will contain everything you need to represent a location on the map such as it's coordinate, name, address, phone numbers, directions. Plenty of information.