HTML and CSS Reference
In-Depth Information
$('body').append('<p><a href="http://www.maxmind.com" target="_blank">IP
to Location Service Provided by MaxMind</a></p>');
}
}
function error(msg) {
alert(msg);
}
Discussion
Get the coordinates from
getCurrentPosition()
and pass them to a
printAddress()
function, which uses the Google Maps API to do the reverse geocoding.
The
printAddress()
function begins by creating a new Google
Geocoder
object. The
Geocoder
object gives us access to the
geocode()
method, which can take in a variety of
options and return information based on them.
In our case, we're using the
google.maps.LatLng()
method to create a new Google
LatLng
object that is passed into
geocode()
in order to get the address. The
geocode()
method is asynchronous, just like
getCurrentPosition()
, so we define an inline Java-
Script function to handle the callback.
The callback's response contains two parameters, one for the results and the other for
the
status
code. If the
status
is
OK
, then it's safe to parse the array of
GeocoderRe
sults
objects stored in the
results
variable. The
results
variable is an array since
Geocoder
may return more than one entry.
Next, check for a
GeocoderResults
object in the first position of the array and, if it exists,
append the
formatted_address
property to the web page's body.
See Also
For more information about reverse geocoding, see
http://code.google.com/apis/maps/
8.4 Converting an Address into Latitude and Longitude
Problem
You want to turn an address into latitude and longitude coordinates.
Solution
Use the Google Maps JavaScript API V3 to turn an address into latitude and longitude,
as shown in
Figure 8-3
. This is called
geocoding
.