Skip to content

Instantly share code, notes, and snippets.

@willu2
Forked from Lmendev/drawSector.js
Created April 18, 2020 20:52
Show Gist options
  • Save willu2/25172fec25d5c26a278c3693317d808c to your computer and use it in GitHub Desktop.
Save willu2/25172fec25d5c26a278c3693317d808c to your computer and use it in GitHub Desktop.
Function to create a semicircle with Google Maps API v3. Coordinated and azimuth data are required. This is an adaptation of https://groups.google.com/forum/#!topic/google-maps-api/rKefiTZPXE8
function drawSector(lat, lng, r, azimuth, width) {
var centerPoint = new google.maps.LatLng(lat, lng);
var PRlat = (r/3963) * (180 / Math.PI); // using 3963 miles as earth's radius
var PRlng = PRlat/Math.cos(lat*((Math.PI / 180)));
var PGpoints = [];
PGpoints.push(centerPoint);
with (Math) {
lat1 = lat + (PRlat * cos( ((Math.PI / 180)) * (azimuth - width/2 )));
lon1 = lng + (PRlng * sin( ((Math.PI / 180)) * (azimuth - width/2 )));
PGpoints.push( new google.maps.LatLng(lat1,lon1));
lat2 = lat + (PRlat * cos( ((Math.PI / 180)) * (azimuth + width/2 )));
lon2 = lng + (PRlng * sin( ((Math.PI / 180)) * (azimuth + width/2 )));
var theta = 0;
var gamma = ((Math.PI / 180)) * (azimuth + width/2 );
for (var a = 1; theta < gamma ; a++ ) {
theta = ((Math.PI / 180)) * (azimuth - width/2 +a);
PGlon = lng + (PRlng * sin( theta ));
PGlat = lat + (PRlat * cos( theta ));
PGpoints.push(new google.maps.LatLng(PGlat,PGlon));
}
PGpoints.push( new google.maps.LatLng(lat2,lon2));
PGpoints.push(centerPoint);
}
var poly = new google.maps.Polygon({
path:PGpoints,
strokeColor: '#4B0082',
strokeOpacity: .2,
map: map
});
poly.setMap(map);
return poly;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment