Reverse geocoding AJAX

Uses Google Maps API v3

by Sam_Butler

HTML

<button type="button" id="button">Click</button>
<input type="text" id="text" placeholder="location">
<input type="text" id="lat" value="53.53679340000001">
<input type="text" id="lng" value="-2.2627769">

JavaScript

$('#button').click(function() {
    var myLat = $('#lat').val();
    var myLng = $('#lng').val();
    var myUrl = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='+myLat+','+myLng+'&sensor=false';
    $.getJSON(myUrl,function(json){
        var myAddress = json.results[0].formatted_address;
        $('#text').val(myAddress);
    });
});