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

Posted by Subesh Pokhrel 20 May, 2008

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.

Categories : Google Maps Tags : , , ,

Comments
May 20, 2008

Hi! How this geo information helps me?

Posted by Pawan Pokhrel
May 20, 2008

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!

Posted by admin
May 20, 2008

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!!!:)

Posted by Ashish
May 20, 2008

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.

Posted by admin
May 20, 2008

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

Posted by sanjeeb
May 20, 2008

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

Posted by sanjeeb
May 20, 2008

good tutorial man…

Posted by princu
May 20, 2008

@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

Posted by admin
May 20, 2008

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

Posted by Prajwal
May 21, 2008

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

Posted by yalamber subba
May 22, 2008

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

Posted by admin
May 26, 2008

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.

Posted by Rupesh Acharya
May 29, 2008

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

Posted by Roshan Bhattarai
June 27, 2008

Great work. Keep it up.

Posted by Ramesh
November 12, 2008

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

Posted by tarang
November 13, 2008

@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!

Posted by Subesh Pokhrel
December 15, 2008

thanks man! i finally understand

Posted by bighippo
Leave a comment

(required)

(required)