Polygons crossing poles and the date line in google maps as3 API
Google provides a sample of how to draw a circle in google maps using the Polygon class here:
http://code.google.com/p/gmaps-samples/source/browse/trunk/basic_wcircle/basicw_circle.htm
The problem is that when crossing the date line or the poles, the polygon draws incorrectly. the following code builds on the method drawCircle in google's implementation. This one is designed to return the points in an array for a specified radius around a LatLng position. Polygons will cross the date line, and if the poly reaches a pole it will draw a hard line across the top of the map instead of breaking into the gray outside the map. Verified working in 1.16 of the API.
private function getPoints(latLng:LatLng, radius:uint):Array {
var circleLatLngs:Array = [];
var circleLat:Number = radius * 0.014483;
var circleLng:Number = circleLat / Math.cos(latLng.lat() * Math.PI / 180);
var numPoints = 100;
var maxLatitude:uint = 85; //equates to the latitude edge in google maps (not 90)
for (var i = 0; i < numPoints + 1; i++) {
var theta:Number = Math.PI * (i / (numPoints / 2));
var vertexLat:Number = latLng.lat() + (circleLat * Math.sin(theta));
var vertexLng:Number = latLng.lng() + (circleLng * Math.cos(theta));
//if the latitude value is outside the maxLatitude value for either hemisphere, lock it to that value.
if (vertexLat > maxLatitude) vertexLat = maxLatitude;
if (vertexLat < -maxLatitude) vertexLat = -maxLatitude;
var vertextLatLng:LatLng = new LatLng(vertexLat, vertexLng, true); //setting true allows values outside 180 degrees, which fixes issues crossing the date line
circleLatLngs.push(vertextLatLng);
}
return circleLatLngs;
}
Hopefully that saves some headaches!



