Javascript 谷歌地图API圆相交检测
我试图弄明白,当两个谷歌地图圆圈(围绕标记)相交或相撞时,是否有可能检测到Javascript 谷歌地图API圆相交检测,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我试图弄明白,当两个谷歌地图圆圈(围绕标记)相交或相撞时,是否有可能检测到 我想做的是,如果两个圆圈相交,我想提出一个事件。但我不确定这是否可行 计算圆心之间的距离,如果它小于两个圆的半径之和,则它们相交。下面是一些JavaScript,可以检测两个圆是否相交 var e = Math; // shortcut for the mathematical function var D2R = e.PI/180.0; // value used for converting d
我想做的是,如果两个圆圈相交,我想提出一个事件。但我不确定这是否可行 计算圆心之间的距离,如果它小于两个圆的半径之和,则它们相交。下面是一些JavaScript,可以检测两个圆是否相交
var e = Math; // shortcut for the mathematical function
var D2R = e.PI/180.0; // value used for converting degrees to radians
Number.prototype.toRadians = function() {
return this * D2R;
};
function distance(lat0,lng0,lat1,lng1){
// convert degrees to radians
var rlat0 = lat0.toRadians();
var rlng0 = lng0.toRadians();
var rlat1 = lat1.toRadians();
var rlng1 = lng1.toRadians();
// calculate the differences for both latitude and longitude (the deltas)
var Δlat=(rlat1-rlat0);
var Δlng=(rlng1-rlng0);
// calculate the great use haversine formula to calculate great-circle distance between two points
var a = e.pow(e.sin(Δlat/2),2) + e.pow(e.sin(Δlng/2),2)*e.cos(rlat0)*e.cos(rlat1);
var c = 2*e.asin(e.sqrt(a));
var d = c * 6378137; // multiply by the radius of the great-circle (average radius of the earth in meters)
return d;
}
function hasIntersections(circle0,circle1){
var center0 = circle0.getCenter();
var center1 = circle1.getCenter();
var maxDist = circle0.getRadius()+circle1.getRadius();
var actualDist = distance(center0.lat(),center0.lng(),center1.lat(),center1.lng());
return maxDist>=actualDist;
}
只要调用HasCrossons,并引用您的圆即可。下面是一个示例,显示两个圆几乎接触(返回false),如果将c1中的0更改为1,它们将接触(返回true)
您好,该函数在控制台中返回一个错误:“UncaughtTypeError:circle0.getCenter不是函数”,您能检查一下吗?因为这个解决方案对我来说也很重要。
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 41.081301, lng: -98.214219},
zoom: 25
});
var c0 = new google.maps.Circle({
strokeOpacity: .1,
strokeWeight: 1,
fillColor: '#0000FF',
fillOpacity: .2,
map: map,
center: {lat:41.082953, lng: -98.215285},
radius: 200
});
var c1 =new google.maps.Circle({
strokeOpacity: .1,
strokeWeight: 1,
fillColor: '#FF0000',
fillOpacity: .2,
map: map,
center: {lat:41.081070, lng: -98.214027},
radius: 34.692866520
});
console.log(hasIntersections(c1,c0));