Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pixel distance in Google Map (API V3) to avoid overlapping labels / overlays

What is the best way to calculate the pixel distance between to coordinates (lat/lon) in a Google Map. I am not asking about the distance in km/miles (therefore I have a library), but pixels on the screen considering the current zoom factor.

Background: I want to draw overlays without overlapping them. So I need to calculate the offset for these positions. So in a lower zoom factor a distance of 500 km can mean the overlays do overlap, while at 50km they don't.

Of course, any other algorithm to avoid overlaps not based on the pixel distance would be appreciated.

-- Update --

Guess is is going in the direction of clustering, will check this:

  1. http://www.appelsiini.net/2008/11/introduction-to-marker-clustering-with-google-maps
  2. http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/examples/advanced_example.html

-- Update --

Most likely I can use this here: http://code.google.com/apis/maps/documentation/javascript/reference.html

google.maps.MapCanvasProjection.fromLatLngToDivPixel

To get the projection I will use the answers here:

  1. Pan point on Google Map to specific pixel position on screen (API v3)
  2. How to call fromLatLngToDivPixel in Google Maps API V3?
like image 709
Horst Walter Avatar asked Aug 17 '11 11:08

Horst Walter


People also ask

Can Google Maps API calculate distance between two points?

The API returns information based on the recommended route between start and end points. You can request distance data for different travel modes, request distance data in different units such kilometers or miles, and estimate travel time in traffic.

How many markers can Google Maps API handle?

As far as I know there is no limit on how many markers you can add to a google-maps based map - however the performance of your map will decrease when you add a lot of them.

How do I limit areas on Google Maps?

You can limit the view able area and zoom level of the google map using limit panning settings. Google map plugin provides easiest way to limit panning/dragging so the map stays within certain bounds using limit panning settings. Step 1 Go to Add or Edit Map and scroll down to 'Limit Panning Settings' section.


1 Answers

First I found that what I am looking for is subject of the clustering libraries / examples:

  1. http://www.appelsiini.net/2008/11/introduction-to-marker-clustering-with-google-maps
  2. http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/1.0/examples/advanced_example.html

The examples showed me a method google.maps.MapCanvasProjection.fromLatLngToDivPixel, which required a projection of the map. Eventually the following topics revealed how to get this projection:

  1. Pan point on Google Map to specific pixel position on screen (API v3)
  2. How to call fromLatLngToDivPixel in Google Maps API V3?
like image 95
Horst Walter Avatar answered Dec 07 '22 23:12

Horst Walter