📜 ⬆️ ⬇️

How to build a sector on Google Maps?

The task was to build a sector on a Google maps (GM) map. As far as I know, this can only be done with the help of direct ones. http://maps.forum.nu/ here are examples of how to build a circle, but this is a bit incompatible to me. Since the circle is constructed according to the following principle: the coordinates of an arbitrary point on the circle are calculated using the formulas:

var PGlat = (PGradius/3963)*r2d; // using 3963 miles as earth's radius
var PGlng = PGlat/Math.cos(lat*d2r);

where r2d and d2r are conversions from radians to degrees and back, respectively ..lat is the latitude of the center of the circle.
I will say at once that this formula is not completely clear to me, but it works and gives the correct coordinates of a point on a circle.
After this, we draw 360 straight lines with a certain step from the obtained point.
var PGsides = 360;
var PGstart = (PGsides%2 == 1) ? 2/PGsides : 1/PGsides;
PGstart = (0.5 - PGstart) * Math.PI;

for (var i=-1; i < PGsides; i++) {
var theta = 2*i*Math.PI/PGsides + PGstart;
PGx = lng + (PGlng * Math.cos(theta));
PGy = lat + (PGlat * Math.sin(theta));
PGpoints.push(new GPoint(PGx,PGy));
}

there is still such an option for constructing a circle, but I did not understand his calculations a little, but everything is done on the same principle
if (circleUnits == 'KM') {
var d = circleRadius/6378.8; // radians
}
else { //miles
var d = circleRadius/3963.189; // radians
}

var lat1 = (PI/180)* center.lat(); // radians
var lng1 = (PI/180)* center.lng(); // radians

for (var a = 0 ; a < 361 ; a++ ) {
var tc = (PI/180)*a;
var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
var x = ((lng1-dlng+PI) % (2*PI)) - PI ; // MOD function
var point = new GLatLng(parseFloat(y*(180/PI)),parseFloat(x*(180/PI)));
circlePoints.push(point);
bounds.extend(point);
}

if (d < 1.5678565720686044) {
circle = new GPolygon(circlePoints, '#000000', 2, 1, '#000000', 0.25);
}
else {
circle = new GPolygon(circlePoints, '#000000', 2, 1);
}
map.addOverlay(circle);


Let's return to the construction of the sector.
Input Values:
lat, lng - center coordinates;
r is the radius;
azimuth - azimuth of the center of the sector (medians of the inscribed triangle) in degrees;
width - the angle of the sector.

The main problem is the coordinates of the first and last points of the sector. In theory, these coordinates are obtained using the following formulas:
PGlat = lat + (r * Math.cos( (azimuth - width/2 )));
PGlon = lng + (r* Math.sin( (azimuth - width/2 )));

PGlat = lat + (r * Math.cos( (azimuth + width/2 )));
PGlon = lng + (r* Math.sin( (azimuth + width/2 )));

But it interferes with r - radius in miles. In theory, it must be converted, since we have to get the value in degrees, but this is the transformation I cannot understand.
')
Maybe someone can help or saw somewhere building a sector on GM. I would appreciate any help in this matter.

Source: https://habr.com/ru/post/65653/


All Articles