A web engineer’s blog
2 Jul
Have you ever used the Google Maps API and found out that you don’t really know the right position where to place the markers? As you know, to do so requires entering in javascript the exact marker’s coordinates, so you go in a trial-and-error loop to try to correct the numbers until you find the right spot, browse for various so called google maps builders hoping that one of them actually gives you the coordinates you need to nail the right place, or modify your code so that outputs in some way the coordinates, find the marker’s position, jot it down, then restore the old script and add the numbers.
During this phase the client calls saying the marker isn’t in the right place, it’s "more to the sea", or "a bit more to the left". You also have the problem that modifying the javascript code makes the map go into the debug mode (outputting coordinates) as long as the right numbers have been found out, even web site readers see them.
Solve the problem one time for all by adding this few lines to your javascript code that uses the Google Maps API (I assume the google map object name is "map", change as needed):
if (document.location.href.indexOf(’mapsdebug’) != -1) {
var markerdebug = new GMarker(map.getCenter()),{draggable:true});
GEvent.addListener(markerdebug, "dragend", function() {
markerdebug.openInfoWindowHtml(markerdebug.getPoint().toString()
)
});
map.addOverlay(markerdebug);
}
This tells the browser that if the URL contains #mapsdebug a new draggable marker appears at the center of the map, that when dropped on a new location outputs in a balloon the point coordinates.
This way you or your client can simply find the marker’s coordinates by adding a snippet to the URL.
Here there is a live example in standard and in debug mode.
Imagine the problem: You approximatively (maybe unintentionally) place a marker in the wrong position, then phone or email the client and say to add #mapsdebug to the map URL, drag the new marker and tell you the numbers.
You place the right coordinates in the javascript and the pinpoint appears into the right position.
Pros of this technique:
Cons:
There’s an alternate technique suggested on a lifehacker (indeed a tech recipes one but this has a screen shot!): Go to the Google Maps website, then center the view on the place you wish to know the latitude and longitude data, then paste this code into the browser bar:
javascript:void(prompt(”,gApplication.getMap().getCenter()));
I prefer mine because it’s more accurate and easily to explain to the website owner if he wants to pinpoint the address himself.
Happy mapping!
9 Responses for "How to find the right point in Google Maps (longitude and latitude)"
The zoom in/out directions are the opposite of Google maps. I found this confusing. Is it because you are in Italia?
You mean in the live example? Because in the article I don’t mention the zoom feature. The notation for zooming here in Italy is not different from the international standard. There’s even the lucky (for me!) event that you found an error in the live example! Did you experience inverted zooming on the live example? Where you using the scroller of the mouse or the controls on screen? If you’re using the scroller, what mouse do you have? What is your browser?
My map object is called ‘map’, I’ve added the code above to the external javascript code used to construct the map, but the map fails to load. Confusing.
http://www.hillarysboatshop.com.au/skins/default/en/google_maps.js
http://www.hillarysboatshop.com.au/cart.php?mode=map
Checked out your website, but the code is commented out so I can’t check with Firebug where the problem is!
I suppose the map fails to load even if you don’t use it in debug mode, so it could be a simple syntax error somewhere.
Could you try uncomment it (or create another page with the code uncommented) so we can try to debug it?
It would be very interesting, indeed.
try changing
var markerdebug = new GMarker(map.getCenter()),{draggable:true});
into
markerdebug = new GMarker(map.getCenter(),{draggable:true});
that worked perfectly Giancarlo, thanks!
I was googling for this kind of solution and found this post. The situation with the client on the phone explaining me where to put the marker is exactly what I am trying to avoid
Thank you very much for the code!
Hi Giancarlo,
Liked the idea in your blog post:
“How to find the right point in Google Maps
(longitude and latitude)”.
My question:
- where exactly do you place your short Javascript code,
in a standard page with a Google Map?
(can you give a simple example?).
I tried looking at the source code of your demo page,
but could not find your JS code anywhere, (needed to perform “the magic”).
Grazie!!
Ray SF
Hi Ray,
try to place the code after the initialization script, to clarify that I call a function named google_buildmap() located in
/lib/google_maps/js/google_maps.js
from the main init function.
Leave a reply