Skip to content

Instantly share code, notes, and snippets.

@bewithdhanu
Last active December 8, 2023 16:17
Show Gist options
  • Save bewithdhanu/5d732c4a923d3105b29d1ad99e9f1459 to your computer and use it in GitHub Desktop.
Save bewithdhanu/5d732c4a923d3105b29d1ad99e9f1459 to your computer and use it in GitHub Desktop.
Google Map JS SDK - Convert window coordinates to LatLng
let x = 100; //relative left position of the point
let y = 200; //relative top position of the point
let projection = map.getProjection();
let scale = Math.pow(2, map.getZoom());
let topRight = projection.fromLatLngToPoint(map.getBounds().getNorthEast());
let bottomLeft = projection.fromLatLngToPoint(map.getBounds().getSouthWest());
const reversePoint = new google.maps.Point(
bottomLeft.x + (x / scale),
topRight.y + (y / scale)
);
return projection.fromPointToLatLng(reversePoint); // converted value of x, y to LatLng
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment