-
Notifications
You must be signed in to change notification settings - Fork 20
Geocoding
Justin Kimbrell edited this page Aug 16, 2014
·
1 revision
Sometimes you may need to geocode using the server-side Geocoding API right from the template. Google Maps for Craft provides you with a method to do so very easily. Geocoding responses will be cached so subsequent requests to same address will not go against your API limit.
{% set response = craft.googleMaps.geocode('Rocky Mountain National Par') %}
{% if response.status == "OK" %}
{% for result in response.results %}
<p>
Addres: {{ result.formatted_address }} <br>
Latitude: {{ result.geometry.location.lat }} <br>
Longitude: {{ result.geometry.location.lng }} <br>
</p>
{% endfor %}
{% else %}
Error: {{ response.status }}
{% endif %}
For more information on the returned response, refer to the API documentation.
{
"results" : [
{
"address_components" : [
{
"long_name" : "Rocky Mountain National Park",
"short_name" : "Rocky Mountain National Park",
"types" : [ "establishment" ]
},
{
"long_name" : "Colorado",
"short_name" : "CO",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Rocky Mountain National Park, Colorado, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.5537873,
"lng" : -105.4935834
},
"southwest" : {
"lat" : 40.1580668,
"lng" : -105.9137138
}
},
"location" : {
"lat" : 40.3427932,
"lng" : -105.6836389
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.5537873,
"lng" : -105.4935834
},
"southwest" : {
"lat" : 40.1580668,
"lng" : -105.9137138
}
}
},
"types" : [ "natural_feature", "park", "establishment" ]
}
],
"status" : "OK"
}