Get latitude and longitude of any location using google map

Nov 19, 2013, by admin

If you are playing with google map, and want to get Latitude and Longitude of any location.
Few simple steps to go.
<script type=”text/javascript” src=”http://maps.google.com/maps?file=api&v=2&key=GOOGLE_MAP_KEY”></script>
<div class=”Mapblock”>
<div style=”height:auto;”>
<meta http-equiv=”X-UA-Compatible” content=”IE=EmulateIE7″ />
<div id=”map” align=”center” style=”height: 276px;width:276px;border: 1px solid #DDDDDD; font-size:12px; font-weight:normal; color:#000000;”></div>
<script type=”text/javascript”>
if( window.addEventListener )
window.addEventListener( ‘load’, load, false );
else
window.attachEvent( ‘onload’, load );
function load() {
//<![CDATA[
var map = new GMap2(document.getElementById(“map”));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(27.137368,75.761718), 5);
var marker = new GMarker(new GLatLng(37.4419, -122.1419), {draggable: true});
map.addOverlay(marker);
GEvent.addListener(map, ‘click’, function(overlay, point) {
if(point)
{
var lati=point.lat(); //alert(lati);
var long=point.lng(); //alert(lati);
var marker= new GMarker(new GLatLng(lati, long),{draggable: true});
map.addOverlay(marker);
marker.openInfoWindowHtml(“Latitude&nbsp;&nbsp;&nbsp;”+lati+”<br>”+”Longitude&nbsp;&nbsp;&nbsp;”+long);
GEvent.addListener(marker, “dragend”, function() {
var point = marker.getPoint();
var lati=point.lat();
var long=point.lng();
marker.openInfoWindowHtml(“Latitude&nbsp;&nbsp;&nbsp;”+lati+”<br>”+”Longitude&nbsp;&nbsp;&nbsp;”+long);
});
}
});
GEvent.addListener(map, ‘singlerightclick’, function(point,src,overlay)
{
if(overlay){
if(overlay != marker) { map.removeOverlay(overlay) }
}
}
);
//]]>
}
</script>
</div>
</div>

  1. Get google map key.You can get from the below link of Google Map.Here I am using version2,in version3 of google map,we don’t need key.
  2. Replace GOOGLE_MAP_KEY with your google map key.
  3. Copy and paste the script in your code.
  4. Set the center of the map (Default center) by replacing the respective Latitude and Longitude.

map.setCenter(new GLatLng(27.137368,75.761718), 5);
Here number 5 is zoom level,you can set this one also.

  1. Run the code.
  2. Click on any point on the map,a popup will automatically show up with the respective Latitude and Longitude(co-ordinates) of that point.
  3. If you want to discard it,don’t worry,just right click on the google map marker.