Tips and Tricks:Fetching Coordinates

From Sobi2Wiki

Jump to: navigation, search

Fetching Geographical Coordinates By Address in the Add/Edit Entry Form of SOBI2

You will find the original text here: Fetching Geographical Coordinates By Address

SOBI2 version:
Works with SOBI2 versions since RC 2.7.2

Requirements:

  • Google API Key entered in SOBI2 View Configuration->Google Maps
  • Custom field for longitude 'field_longitude' assigned in SOBI2 View Configuration->Google Maps
  • Custom field for latitude 'field_latitude' assigned in SOBI2 View Configuration->Google Maps
  • Custom field for post code 'field_postcode'
  • Custom field for city 'field_city'
  • Custom field for street 'field_street'
  • Custom field for country 'field_country'
  • Custom field of field type "text code" (formerly called custom code) to insert the script below.

View Configuration->Google Maps

Instructions:

  • If not already exist, create the fields for post code, city, street and country in the "Custom Fields Manager". Be sure that the field names are the same as shown above.
  • Create the fields for longitude and latitude and assign them to the map in SOBI2 View Configuration->Google Maps (see image)
  • Enter the Google API Key in SOBI2 View Configuration->Google Maps (see image)
  • Create a new field in the "Custom Fields Manager" called for example "Fetch Coordinates". Set the field type to "text code" and insert the code below in the "Text Code" area.
  • If necessary, adapt the code to your needs.


<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+ "&callback=getCoordinates&output=JSON&key="+apiKey;
		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"/>

NOTE: if you unpublish and then attempt to republish this field from the list of custom fields you may encounter and error stating "you must edit this field to publish". But find the publish buttom is not available when you click on the field.

To work around this simply change the field type temporarily to anything else except custom - all the other options including "publish" will reappear so click the radio button for "publish" then set field type back to custom and save.

Personal tools