Reputation: 1067
I have a polygon with an approximate area of 10 000 m2
I'm trying to determine on which zoom level the polygon became almost invisible, means - the number of pixels it takes to draw the polygon is negligible, and remove it from map to save resources.
Since I already have a real polygon area value, how can I calculate the area of a polygon drawn on a map, in pixels, for specific zoom level?
Upvotes: 2
Views: 2583
Reputation: 627
Version with the polygon and not the bounds, google maps:
var square_meters = google.maps.geometry.spherical.computeArea(yourPolygon.getPath());
var init_resolution = 256 / (2 * Math.PI * 6378137);
var zoom = map.getZoom();
var resolution = init_resolution * Math.pow(2,zoom);
var area_factor = Math.pow(resolution,2);
var square_pixels = square_meters * area_factor;
256
is the TileSize
of the map and 6378137
the radius of the earth in meters. You get the area of your polygon in squared pixels. You can compare this to your maps dimensions in squared pixels (height * length
of div
);
Upvotes: 2
Reputation: 9407
In order to simplify the problem I'd suggest working with the polygon's bounds rather than the polygon itself. That way you always deal with a rectangle and avoid the trouble of calculating sizes of irregular polygons. With that in mind, and assuming your polygon is simple and has only one path, you could do something like this:
var points = poly.getPath();
var bounds = new google.maps.LatLngBounds();
for (var n = 0; n < points.length ;n++){
bounds.extend(points[n]);
}
var SW = bounds.getSouthWest();
var NE = bounds.getNorthEast();
var proj = map.getProjection();
var swPx = proj.fromLatLngToPoint(SW);
var nePx = proj.fromLatLngToPoint(NE);
var pixelWidth = (nePx.x - swPx.x)* Math.pow(2, map.getZoom());
var pixelHeight = (nePx.y - swPx.y)* Math.pow(2, map.getZoom());
Upvotes: 6