Friday 16 November 2012

Getting the Longitude and Latitude from a particular address or postcode with Google Maps Api v3.

Google API v3 supports unlimited calls, and doesn’t need the complexity of creating keys like the version 2, Bing maps or Yahoo maps.

I have created a small, but useful function to get the Latitude and Longitude of a particular address or postcode. It looks pretty simple, but this is a nice start if you want to build something big with the Google API.

In this example I target the Buckingham Palace, in London (UK).

<html>
<head>
    <script type='text/javascript' src='http://maps.google.com/maps/api/js?sensor=false'></script>
    <script type="text/javascript">
        //########################################
        function GetLongitudeAndLatitude(_sAddress) {
            var mygc = new google.maps.Geocoder();
            mygc.geocode({ 'address': _sAddress }, function (results, status) {
                alert("latitude : " + results[0].geometry.location.lat());
                alert("longitude : " + results[0].geometry.location.lng());
            });
        }
        //########################################
    </script>
</head>
<body onload="GetLongitudeAndLatitude('SW1A 1AA');">
</body>
</html>