0

我正在使用半正弦公式和 google.maps.Polyline 在地图上画一个圆圈。我的代码中有一个错误,导致圆圈被画成一条线。造成这种情况的错误是什么,我该如何纠正?(我需要使用折线绘制我的圆圈,以便我可以使用圆圈的点来确定给定位置是否在圆圈内。因此,我没有使用 google.maps.Circle)

见下面的代码:

var address=document.getElementById("address").value;
var radius=document.getElementById("radius").value;
var latitude=40;
var longitude=0;
geocoder.geocode( { 'address': address}, function(results, status){
if (status==google.maps.GeocoderStatus.OK){
latlng=(results[0].geometry.location);
latitude=latlng.lat();
longitude=latlng.lng();
}   

else{
    alert("Geocode was not successful for the following reason: " + status);
}
});





//Degrees to radians 
  var d2r = Math.PI / 180;

  //  Radians to degrees
 var r2d = 180 / Math.PI;

 // Earth radius is 3,963 miles
 var cLat = (radius / 3963) * r2d;

 var cLng = cLat / Math.cos(latitude * d2r);


  //Store points in array 
  var points = [];
alert("declare array");

  var bounds= new google.maps.LatLngBounds();

  // Calculate the points
  // Work around 360 points on circle
  for (var i=0; i < 360; i++) {

  var theta = Math.PI * (i/16);

  // Calculate next X point 
  circleY = longitude + (cLng * Math.cos(theta));            
   // Calculate next Y point 
  circleX = latitude + (cLat * Math.sin(theta));
    // Add point to array 
    var aPoint=new google.maps.LatLng(circleX, circleY);
    points.push(aPoint);
    bounds.extend(aPoint);

 }
 points.push(points[0]);//to complete circle

var colors=["#CD0000","#2E6444","#003F87" ];

var Polyline_Path = new google.maps.Polyline({
path: points,
strokeColor: colors[count],
// color of the outline of the polygon
strokeOpacity: 1,
// between 0.0 and 1.0
strokeWeight: 1,
// The stroke width in pixels
fillColor: colors[count],
fillOpacity: 0
});
Polyline_Path.setMap(map);
4

2 回答 2

1

您在循环中的 theta 值需要从 0 变为 2*PI 才能创建一个完整的圆圈。您的值从 0 变为 22.5*PI。这意味着你要绕圈 10.25 圈,最后画一条线,从绕圈四分之一处到你开始的点:这就是你所说的线。

尝试使用:

var theta = Math.PI * (i / 180);

尽管您可能还想减少点数:360 段对于一个圆来说很多。我发现 32 通常绰绰有余。

于 2011-07-19T22:26:06.173 回答
1

上面的答案是正确的,但是你说你不能使用 API 中的圆,因为你想在多边形检查中做一个点。您应该使用 Circle 类并进行距离检查以了解该点是否在圆圈中。您可以使用距离实现或使用 API (google.maps.geometry.spherical.computeDistanceBetween)。

于 2011-09-06T19:55:29.483 回答