Sobi2
FAQ
Google Maps & Way Search
How to fetch geo coordinates in add/edit entry form (by address) |
Located in Google Maps & Way Search Tagged with Google Map, fetch coordinates, geo |
|
|
|
| Written by Sigrid Suski |
| Last updated on Friday, 16 April 2010 |
Fetching Geographical Coordinates by address in the Add/Edit Entry form of Sobi2.
Works with Sobi2 versions since RC 2.7.2 Requirements:
Instructions:
<script type="text/javascript" language="JavaScript"> /* adjust here the field names if they are not correct */ var apiKey = "{googleApiKey}"; var postalCodeField = 'field_postcode'; var cityField = 'field_city'; var streetField = 'field_street'; var countryField = 'field_country'; var latitudeField = 'field_latitude'; var longitudeField = 'field_longitude'; function fetchCoordinates() { /* here you should not change anything */ var gRequest = null; var postalcode = document.getElementById(postalCodeField).value; var city = document.getElementById(cityField).value; var street = document.getElementById(streetField).value; var country = document.getElementById(countryField).value; if(postalcode == '' || city == '' || street == '' || country == '' ) { /* you can change the error message here */ alert("Please fill in the address fields first"); } else { var gRequest = "http://maps.google.com/maps/geo?q=" +street+ "+" +postalcode+ "+" +city+ "+" +country+ "&output=json&key="+apiKey+"&callback=getCoordinates"; var scriptObj = document.createElement("script"); scriptObj.setAttribute("type", "text/javascript"); scriptObj.setAttribute("src", gRequest); document.getElementsByTagName("head").item(0).appendChild(scriptObj); } } function getCoordinates(data) { switch(data.Status.code) { case 610: /* you can change the error message here */ alert("Api key not valid: {googleApiKey}"); break; case 603: case 602: case 601: case 500: /* you can change the error message here */ alert("Cannot get coordinates for this address"); break; case 200: document.getElementById(latitudeField).value = data.Placemark[0].Point.coordinates[1]; document.getElementById(longitudeField).value = data.Placemark[0].Point.coordinates[0]; break; } } </script> <!-- You can change the "value" (The label on the button) here --> <input type="button" class="button" onclick="fetchCoordinates();" value="Fetch Coordinates"/> ![]() |
| Published on Tuesday, 29 January 2008. Visited 29607 times. |
| < Prev | Next > |
|---|
| Powered by Sigsiu.NET | ![]() |
Visitors: