LinkedIn Twitter RSS Reset

How To Get Latitude and Longitude of a Place Using Google Maps

I was so frustrated during one project where I need to find out the geological latitude and longitude of the place, with the help of Google Maps. So I googled a lot and came up with a function function (overlay,point).On calling this function on mouse click event the latitude and longitude are returned as point.lat() for latitude and point.lng() for longitude. This is the source code just the javascript part.
[UPDATE DEMO]

  <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAPHLcOOGHX2-uLk3K8q1nMRTkUAbhgKwL1jWWfpv-KGJeCrct7hTsLLnZdnZjzehmRIkaePagQvKNbw" type="text/javascript"></script>
 <script type="text/javascript">
    //<![CDATA[

    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 4);

	var marker = new GMarker(new GLatLng(37.4419, -122.1419), {draggable: false});
	map.addOverlay(marker);
	GEvent.addListener(map, 'click', function(overlay, point) {
		  var lati=point.lat();
		  var long=point.lng();
		  var marker= new GMarker(new GLatLng(lati, long),{draggable: false});
		  map.addOverlay(marker);
		  marker.openInfoWindowHtml("Latitude:"+lati+"<br />"+"Longitude:"+long);
		 });

	//]]>
    </script>

What I have done here is I have displayed the latitude and longitude on a information window of a GMarker. GMarker represents a point on the map. I may put something about GMarker later on.

17 Responses to “How To Get Latitude and Longitude of a Place Using Google Maps”

  1. Pawan Pokhrel
    May 20, 2008 at 5:37 am #

    Hi! How this geo information helps me?

  2. admin
    May 20, 2008 at 6:09 am #

    If You want to show the location of your system user plotted over Gmap, then you should ask the user to register their Latitude and Longitude and save it in the database.. so you need this right!

  3. Ashish
    May 20, 2008 at 10:36 am #

    Good…It might be helpful…. We had to similar sort of thing during our travel-xp project…I think we did it using name of the country….passing it to some function…But we had not posted as u did.

    This might be good reference all wanting to do that!!!:)

  4. May 20, 2008 at 10:49 am #

    I think you did not get the point here. It actually shows the latitude and longitude upto 12 decimal digits (i think)of any point on the globe not only one lat and lng of a country.

  5. May 20, 2008 at 2:38 pm #

    Hey this is good information. I will sure use it if required in my projects. nice work.

  6. May 20, 2008 at 2:40 pm #

    It will be helpful to other , if you post with the ready to use source code.

  7. princu
    May 20, 2008 at 2:49 pm #

    good tutorial man…

  8. May 20, 2008 at 4:40 pm #

    @sanjeeb,
    If you’d like to have a ready to use source code…then copy.. the code of first post

    http://subesh.com.np/2008/05/google-map-with-zoom-and-map-type/

    and replace its script with this script you will see the working demo. I will be aware now on to post a ready to use code..:D

  9. May 20, 2008 at 5:02 pm #

    ummm.. done a great job.. keep it up..stand still … push more harder.. heheh dont take the other way :P

  10. May 21, 2008 at 4:53 pm #

    There are some classes to auto calculate the latitude and longitude based on city, state, zipcode and country. It will be better if we can auto calculate the latitude and longitude first and show them the option to change the latitude and longitude with marker. you can get the class here: http://www.phpclasses.org/browse/package/3827.html

  11. May 22, 2008 at 4:53 am #

    @yalamber,
    thanks for the comment… i will look forward to it…

  12. Rupesh Acharya
    May 26, 2008 at 7:18 pm #

    good work man…. sharing ideas. It’s quiet an informative one. it rooted at least some knowledge about embeding google maps in a web page to me.

  13. May 29, 2008 at 4:05 pm #

    hope to get more tips on the google maps from you man……

  14. Ramesh
    June 27, 2008 at 7:48 am #

    Great work. Keep it up.

  15. November 12, 2008 at 3:29 pm #

    Hi
    I m working on the project which locates ip address on map using google
    which is known to u.will u plz tell me how to retrieve data abt ip address like lat. long. and other information.
    thank u for abv useful information

  16. November 13, 2008 at 6:16 am #

    @tarang,
    Please Download the Demo script. for From Here and go through it… I think that will be your project accomplished. :D .. Feel Free to ask any other help required!

  17. December 15, 2008 at 6:36 am #

    thanks man! i finally understand

Leave a Comment