Google maps api 3 谷歌地图v3绘图圈使用哈弗斯公式和多段线

Google maps api 3 谷歌地图v3绘图圈使用哈弗斯公式和多段线,google-maps-api-3,geometry,polyline,Google Maps Api 3,Geometry,Polyline,我正在使用haversine公式和google.maps.Polyline在地图上画一个圆。我的代码中有一个错误,导致画圆时有一条线穿过它。造成这种情况的错误是什么?我如何纠正它?(我需要使用多段线绘制圆,以便使用圆的点确定给定位置是否在圆内。因此,我不使用google.maps.circle) 见下面的代码: var address=document.getElementById("address").value; var radius=document.getElementById("rad

我正在使用haversine公式和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);
var address=document.getElementById(“地址”).value;
var radius=document.getElementById(“radius”).value;
纬度=40;
var经度=0;
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
latlng=(结果[0]。几何体。位置);
纬度=latlng.lat();
经度=latlng.lng();
}   
否则{
警报(“地理编码因以下原因未成功:“+状态”);
}
});
//度到弧度
var d2r=Math.PI/180;
//弧度
var r2d=180/Math.PI;
//地球半径为3963英里
var cLat=(半径/3963)*r2d;
var cLng=cLat/Math.cos(纬度*d2r);
//在数组中存储点
var点=[];
警报(“声明数组”);
var bounds=new google.maps.LatLngBounds();
//计算积分
//围绕圆上的360点工作
对于(变量i=0;i<360;i++){
varθ=数学PI*(i/16);
//计算下一个X点
圆=经度+(cLng*Math.cos(θ));
//计算下一个Y点
circleX=纬度+(cLat*Math.sin(θ));
//将点添加到阵列
var aPoint=new google.maps.LatLng(circleX,circleY);
点。推(点);
扩展(aPoint);
}
点。推(点[0])//圆
变量颜色=[“#CD0000”、“#2E6444”、“#003F87”];
var Polyline_Path=新建google.maps.Polyline({
路径:点,
strokeColor:颜色[计数],
//多边形轮廓的颜色
频闪不透明度:1,
//介于0.0和1.0之间
冲程重量:1,
//笔划宽度(以像素为单位)
fillColor:颜色[计数],
填充不透明度:0
});
多段线路径设置图(map);

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

尝试使用:

varθ=数学π*(i/180)


尽管您可能还希望减少点的数量:对于一个圆来说,360段是很多的。我发现32个通常就足够了

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