Getting pixel distance from a Marker to a LatLng

2470 views
Skip to first unread message

Tim

unread,
Jun 25, 2010, 1:58:17 PM6/25/10
to Google Maps JavaScript API v3
Hi everyone.

I have a pile of LatLng locations, and I'm putting Markers on each
one. This is easily enough done. But what I really want to do is
merge Markers if they overlap, so I need the distance in pixels
between markers, or between LatLng locations.

From looking at the API and other helps, I think I need the
fromLatLngToContainerPixel() method in MapCanvasProjection. OK. Fair
enough. So how do I get a MapCanvasProjection object? Apparently it
only comes from an OverlayView object with the method
getProjection(). OK. Fair enough. But how in the world do I get an
OverlayView object? Now I'm stumped. Any suggestions?

I've tried the basic map.getProjection().fromLatLngToPoint(...), but
this result doesn't change when I zoom the map.

Thanks in advance.

--
Tim

Rossko

unread,
Jun 25, 2010, 4:55:20 PM6/25/10
to Google Maps JavaScript API v3
> I have a pile of LatLng locations, and I'm putting Markers on each
> one.  This is easily enough done.  But what I really want to do is
> merge Markers if they overlap, so I need the distance in pixels
> between markers, or between LatLng locations.

Have you looked at the existing Clusterer?
http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/examples/advanced_example.html

William

unread,
Jun 25, 2010, 8:40:34 PM6/25/10
to Google Maps JavaScript API v3
On Jun 26, 3:58 am, Tim <koop...@gmail.com> wrote:
> But how in the world do I get an
> OverlayView object?  

see the following example map which implements the most basic
OverlayView.
It returns the div and container pixel coordinates when you click on
the map

http://www.william-map.com/20100416/1/map.htm

>
> I've tried the basic map.getProjection().fromLatLngToPoint(...), but
> this result doesn't change when I zoom the map.
>

see the following example which demonstrates the use of this function.
The function returns floating point pixel values between 0 and 255,
and needs to be scaled according to the zoom (multiply by 2^zoom),
then rounded to integers.

var worldCoordinate = projection.fromLatLngToPoint(chicago);
var pixelCoordinate = new google.maps.Point(
worldCoordinate.x * Math.pow(2, map.getZoom()),
worldCoordinate.y * Math.pow(2, map.getZoom()));

http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html

...
Reply all
Reply to author
Forward
0 new messages