In order to collect details of the site that we are looking for, we will need advanced Geocoding.
It is very simple to use. Basically, it consists of passing a place as parameter and collecting all that the Google API provides to us. There are two ways to execute this action:
- If we have a GMap instantiated in our page, all we have to do is: GMap1.getGeoCodeRequest(...) [geoCodeRequest is obsolete since version 3]
- But we can also accede to a static method that does not require a GMap on the. The only thing to consider is that we will have to pass our Google API Key as a parameter: GMap.geoCodeRequest(...);
The static method returns two different data types. On one hand we have theGeocode type (structure the same way as the GoogleMaps XML is), and on the other hand we have an string from which we can select it's output type (xml, kml,json y csv).
Here are the parameters we could set to the methods:
- Query: the location we are searching.
- Coordinates: you can search a GLatLng instead of a location.
- GoogleKey: only necessary if we use the static methods.
- Premier: instead of the GoogleKey, if you have a Google Maps Api premier account. (only needed on static methods)
- GeoCode.outputEnumoutput: the kind of output we want (xml, kml, json y csv). A Geocode element will be returned if the output is not set.
- baseCountryCode: setting the country code we will see the results near it.
- viewport: using GLatLngBounds sets the </span>relevant area<span> where we want to see the results.
We can access to the state message of the search in status.code (for example to know an error code). We could find the description of the state message code here:
http://code.google.com/apis/maps/documentation/reference.html#GGeoStatusCode
The Geocode class is structured in an identical way to the XML that Google provides to us. Let us see an example of the XML from Google if we looked for "1600 amphitheatre mtn view ca". We can compare it with the code example that and we will see that the classes and their properties are analogous to the XML:
<kml>
<Response>
<name>1600 amphitheatre mtn view ca</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark>
<address>
1600 Amphitheatre
Pkwy, Mountain View, CA 94043, USA
</address>
<AddressDetails Accuracy="8">
<Country>
<CountryNameCode>US</CountryNameCode>
<AdministrativeArea>
<AdministrativeAreaName>CA</AdministrativeAreaName>
<SubAdministrativeArea>
<SubAdministrativeAreaName>Santa Clara</SubAdministrativeAreaName>
<Locality>
<LocalityName>Mountain View</LocalityName>
<Thoroughfare>
<ThoroughfareName>1600 Amphitheatre Pkwy</ThoroughfareName>
</Thoroughfare>
<PostalCode>
<PostalCodeNumber>94043</PostalCodeNumber>
</PostalCode>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea>
</Country>
</AddressDetails>
<Point>
<coordinates>-122.083739,37.423021,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>