4

I want to retrieve the top and left position of marker (in pixels) in google map api v3. How can this be done? I know that there are some hints on using fromLatLngToDivPixel(), but may I know how it works? example?

Tomas
  • 57,621
  • 49
  • 238
  • 373
davidlee
  • 5,611
  • 17
  • 56
  • 82
  • Similar question: http://stackoverflow.com/questions/1538681/how-to-call-fromlatlngtodivpixel-in-google-maps-api-v3 – dmitry Nov 22 '11 at 19:16
  • Do you mean the pixel coordinates within the div? Please update the question so that it's clear. – Tomas Nov 23 '11 at 09:54

2 Answers2

5

You mean the coordinates in pixels within the div? Look at http://qfox.nl/notes/116

overlay = new google.maps.OverlayView();
overlay.draw = function() {};
overlay.setMap(map);
var point = overlay.getProjection().fromLatLngToContainerPixel(latLng); 

or

var point = overlay.getProjection().fromLatLngToDivPixel(latLng);

Ugly indeed. Much easier in v2, where these methods were directly methods of GMap2 object. Another flaw of google api v3!

Tomas
  • 57,621
  • 49
  • 238
  • 373
  • 1
    Do you need to initialize a new overlay every time you want to get a pixel reading? Or can you reuse the same overlay (even if the map has changed zooms)? – bennlich May 06 '13 at 21:25
1

If you facing problem having the overlay undefined, that's because the object is created after the map is idle.

Better call this within event listener, full explanation here https://stackoverflow.com/a/6657723/4026345

Community
  • 1
  • 1
Johnny Vietnam
  • 196
  • 1
  • 6