how to "fromDivPixelToLatLng"?

1,296 views
Skip to first unread message

BryanFE

unread,
Jul 29, 2009, 6:30:32 PM7/29/09
to Google Maps JavaScript API v3
I have some legacy Javascript code which is tracking mouse movements,
and I need to do something on the map when the user moves the mouse
over a region.

I can trap the mouse movements easily enough, but I need to convert
browser coordinates into Lat/Lng based upon the viewport.

The docs mention the "fromDivPixelToLatLng" method, but that method is
on the MapCanvasProjection class, and I can't for the life of me how
to find this object.

Question: Given a Map object, how do I determine it's
MapCanvasProjection object (so that I can call fromDivPixelToLatLng)?
Or, is there any better way to translate browser mouse position into
Lat/Lng?

This is with the new V3 API.

Thank you,

Bryan

Esa

unread,
Jul 29, 2009, 7:48:54 PM7/29/09
to Google Maps JavaScript API v3


On Jul 30, 1:30 am, BryanFE <br...@strategoit.com> wrote:

> The docs mention the "fromDivPixelToLatLng" method, but that method is
> on the MapCanvasProjection class, and I can't for the life of me how
> to find this object.

By getProjection() method of OverlayView() class. So you have to
create a dummy OverlayView.

This is a trick from source code of KeyDragZoom library by Nianwei Liu
& Gary Little:


/**@private
* In V3 it is quite hard to gain access to Projection and Panes.
* This is a helper class
* @param {google.maps.Map} map
*/
function ProjectionHelperOverlay(map) {
google.maps.OverlayView.call(this);
this.set_map(map);
}
ProjectionHelperOverlay.prototype = new google.maps.OverlayView();
ProjectionHelperOverlay.prototype.draw = function () {
if (!this.ready) {
this.ready = true;
google.maps.event.trigger(this, 'ready');
}
};

http://google-maps-utility-library-v3.googlecode.com/svn/tags/keydragzoom/1.0/

By the way, the line
google.maps.OverlayView.call(this);
is no more needed with the current version of api.

Esa

unread,
Jul 29, 2009, 7:51:13 PM7/29/09
to Google Maps JavaScript API v3
Correction. Of course it is:

get_projection()

Nianwei

unread,
Jul 30, 2009, 5:54:59 AM7/30/09
to Google Maps JavaScript API v3
In reality when you need to do something with mouse position, most
likely you are talking about container position instead of the
floating div position(which can be a strange number and not very
useful). In V3, there is no fromContainerPixelToLatLng anymore, so you
will need an offset calculation. Check out the section on the code
that handles pixel offset for container.
http://google-maps-utility-library-v3.googlecode.com/svn/tags/scrollwheelzoom/1.0/src/scrollwheelzoom.js

bratliff

unread,
Aug 1, 2009, 12:17:21 PM8/1/09
to Google Maps JavaScript API v3
I have built a simple example:

www.polyarc.us/polycluster/location.html

I suspect Google will add a "mousemove" event listener for the map
canvas.

Esa

unread,
Aug 1, 2009, 12:38:57 PM8/1/09
to Google Maps JavaScript API v3
@Bratliff

Great work but something is still missing. Clicking or dragging with
FF3.5 recenters to an undefined position.

bratliff

unread,
Aug 1, 2009, 2:40:19 PM8/1/09
to Google Maps JavaScript API v3
Unfortunately, it is incompatible with Google's "click" event listener
which is why Google's "mousemove" event listener is required. It
recenters the map at the Lat/Lon coordinates you clicked. It behaves
like a double click without zooming. I realize it is a kludge but I
like to be able to click to center the map without zooming. I lifted
the code from an old static map demo. You can delete the last line of
the "Click" function.
Reply all
Reply to author
Forward
0 new messages