Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do i convert Lat and Long to uk postcode with Javascript
    text
    copied!<p>I'm trying to make use of HTML5 geolocation for a project I'm working on.</p> <p>It seems pretty straighforward to get the Lat and Long of where a user is, via geolocation.</p> <p>Problem is, I need to convert this to a UK postcode, and am strugging as I'm trying to learn javascript.</p> <p>The code I have working is:</p> <pre><code>if (navigator.geolocation) { var timeoutVal = 10 * 1000 * 1000; navigator.geolocation.getCurrentPosition( displayPosition, displayError, { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 } ); } else { alert("Geolocation is not supported by this browser"); } function displayPosition(position) { alert("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude); var Lat = position.coords.latitude; var Long = position.coords.longitude; var inputField = document.getElementById("addressInput"); inputField.value = Lat + Long; } function displayError(error) { var errors = { 1: 'Permission denied', 2: 'Position unavailable', 3: 'Request timeout' }; alert("Error: " + errors[error.code]); } </code></pre> <p>I've found this site, whihc does exactly the kind of thing I want to achieve: <a href="http://www.latlong.net/Show-Latitude-Longitude.html" rel="nofollow">http://www.latlong.net/Show-Latitude-Longitude.html</a></p> <p>Can anyone give me some tips on how to get this working?</p> <p>Any advice would be great</p> <p>thanks in advance</p> <p>========================= Amended code:</p> <pre><code> //var long = '50.**************', lat = '0.**************' var Lat=''; var Long=''; var coordsObj = {coords:{latitude:Lat, longitude:Long}}; if (navigator.geolocation) { var timeoutVal = 10 * 1000 * 1000; navigator.geolocation.getCurrentPosition( displayPosition, displayError, { enableHighAccuracy: true, timeout: timeoutVal, maximumAge: 0 } ); } else { alert("Geolocation is not supported by this browser"); } function displayPosition(position) { console.log(position, position.coords) console.log("Latitude: " + position.coords.latitude + ", Longitude: " + position.coords.longitude); var Lat = position.coords.latitude; alert(Lat); var Long = position.coords.longitude; alert(Long); var inputField = document.getElementById("addressInput"); inputField.value = Lat + Long; return [Lat, Long]; } function displayError(error) { var errors = { 1: 'Permission denied', 2: 'Position unavailable', 3: 'Request timeout' }; alert("Error: " + errors[error.code]); } function reverseGeoLookup(lon, lat) { var req = new XMLHttpRequest() req.open("GET", "http://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+lon+"&amp;sensor=true", true) req.onreadystatechange = function() { if(req.readyState == 4) { var result = JSON.parse(req.response).results for(var i = 0, length = result.length; i &lt; length; i++) { for(var j = 0; j &lt; result[i].address_components.length; j++) { var component = result[i].address_components[j] if(~component.types.indexOf("postal_code")) { var out = document.getElementById('output') out.innerHTML += component.long_name } } } } } req.send() } var latlng = displayPosition(coordsObj) reverseGeoLookup.apply(this, latlng) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload