Robbie Done
Robbie Done

Reputation: 1157

Google Maps + Google Local Search generate lat/long from uk postcode

Is there anyway i can put 2 hidden fields in a form and automatically put the Lat/Long into them from the user inputting a post code?

I don't really want to rely on the user dragging a marker on the Google map to plot the location.

Thanks in advance

Robbie

Upvotes: 0

Views: 4468

Answers (3)

Robbie Done
Robbie Done

Reputation: 1157

Got it sorted by mashing this page - http://gmaps-samples-v3.googlecode.com/svn/trunk/geocoder/getlatlng.html

Works really well.

Upvotes: 1

KJYe.Name
KJYe.Name

Reputation: 17169

I would use JavaScript to control the form submission, and basically include two hidden fields within the form for lat/long. So, after user submits the address/zip code and etc basically use that information and query Google's Geocoding API and retrive the lat/long you are after. After the query, you can insert the queried Lat/Long into the hidden fields's values and then submit using JavaScript.

hidden field in the form and the attach function to the onclick on the submit button of the form:

<input type='hidden' name='lat' id='lat' value='' />
<input type='hidden' name='lng' id='lng' value='' />
<input type="button" value="Send" onClick="javascript:querieBeforeSubmit();">

This is the JavaScript that queries geoapi and insert value into hidden field

  <script type='text/javascript'>
    function querieBeforeSubmit(){
         //queries Google Geoapi w/ address etc
         var lat = //retrived lat
         var lng = //retrived lng
         document.getElementById('lat').value = lat;
         document.getElementById('lng').value = lng;
         form.submit();
    }
    </script>

Upvotes: 1

oliland
oliland

Reputation: 1299

To convert a UK postcode to a Latitude and Longitude, use Google's Geocoding API.

http://maps.googleapis.com/maps/api/geocode/json?address=POSTCODE&sensor=false

You will receive a JSON response which you can easily parse with JavaScript to populate your hidden fields.

Upvotes: 3

Related Questions