Convert LatLng to Point and Point to LatLng in Google Maps API v3

Having googled this, I found code to go one direction, but not the other.  Here’s both.

function latLngToPoint(latLng) {
 var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
 var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
 var scale = Math.pow(2, map.getZoom());
 var worldPoint = map.getProjection().fromLatLngToPoint(latLng);
 return new google.maps.Point((worldPoint.x - bottomLeft.x) * scale, (worldPoint.y - topRight.y) * scale);
}

function PointTolatLng(point) {
 var topRight = map.getProjection().fromLatLngToPoint(map.getBounds().getNorthEast());
 var bottomLeft = map.getProjection().fromLatLngToPoint(map.getBounds().getSouthWest());
 var scale = Math.pow(2, map.getZoom());
 return map.getProjection().fromPointToLatLng(new google.maps.Point((point.x / scale) + bottomLeft.x, (point.y / scale) + topRight.y));
}

One thought on “Convert LatLng to Point and Point to LatLng in Google Maps API v3”

Leave a Reply

Your email address will not be published. Required fields are marked *