shylent
shylent

Reputation: 10086

Getting LatLng of a certain pixel within the map container

How can one determine which LatLng a certain pixel (not determined by clicking!) in the map canvas div corresponds to? Say, I have x and y "offset" of a pixel within the map canvas div and I want to find out a LatLng this pixel corresponds to (f.ex., to place a marker on it).

Upvotes: 0

Views: 879

Answers (1)

plexer
plexer

Reputation: 4622

You need to implement a Javascript class that extends google.maps.OverlayView (http://code.google.com/apis/maps/documentation/javascript/reference.html#OverlayView).

When the map is panned, the draw() method of your class will be called. Within that method you can call getProjection() to get a MapProjection object, and then use MapProjection.fromContainerPixelToLatLng to convert a pixel (relative to the top left of the map) to a LatLng.

The reason that this is so complex is that it's almost certainly not the right thing to be doing. Why do you need to place something relative to the map in pixel space? Users of maps tend to be interested in "real world" space (e.g. points on the surface of the Earth).

Upvotes: 1

Related Questions