dgrat
dgrat

Reputation: 2254

GMaps JavaScript API: How to determine the screen position of markers/objects

I want to implement a function to snap in markers if hovered and if they are close to each other. For this I need to read the location of the window and not the geo coordinates. How is this possible?

Upvotes: 0

Views: 179

Answers (1)

dgrat
dgrat

Reputation: 2254

I found that:

  /**
  * @param {google.maps.Map} map
  * @param {google.maps.LatLng} latlng
  * @param {int} z
  * @return {google.maps.Point}
  */
  var latlngToPoint = function(map, latlng, z){
    var normalizedPoint = map.getProjection().fromLatLngToPoint(latlng); // returns x,y normalized to 0~255
    var scale = Math.pow(2, z);
    var pixelCoordinate = new google.maps.Point(normalizedPoint.x * scale, normalizedPoint.y * scale);

    //console.log('Position: ' + pixelCoordinate.x + "; " + pixelCoordinate.y);
    return pixelCoordinate; 
  };

Upvotes: 1

Related Questions