我的网站上有一张 map ,人们可以利用它来提交一个区域以供营销考虑。我们终于将其大部分更新为新的 v3 API,但我似乎缺少一个功能。当您编辑圆形或多边形时, map 下方显示的半径/面积不会随着编辑进行调整。我认为问题出在这段代码上,但我不确定。有任何想法吗? (Working example here)
var map;
var poly;
function initialize() {
map = new google.maps.Map(
document.getElementById('map'), {
center: new google.maps.LatLng(40.3749, -75.2988),
zoom: 15,
disableDoubleClickZoom: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
//map.clearOverlays();
//map.enableGoogleBar();
featureTable_ = document.getElementById("featuretbody");
select("hand_b");
}
google.maps.event.addDomListener(window, 'load', initialize);
function calcCircleArea(radius)
{
var area = 0;
area = (Math.PI * (radius * radius));
return area;
}
function updateCircle(circle,point)
{
circle_bounds = circle.getBounds();
c_vertex = circle_bounds.getCenter();
var distance = c_vertex.distanceFrom(point);
circle.setMap(null);
circle = drawCircle(c_vertex, distance,20, '#ff0000', 1, 0.5, '#ff0000', 0.5);
}
function drawCircle(center, radius, nodes, line_color, liWidth, line_opacity, fill_color, fill_opacity)
{
// calc km/degree
var latC = center.distanceFrom(new google.maps.LatLng(center.lat()+0.1, center.lng()))/100;
var lngC = center.distanceFrom(new google.maps.LatLng(center.lat(), center.lng()+0.1))/100;
//calc circle points
var points = [];
var step = parseInt(360/nodes)||10;
for(var i=0; i<=360; i+=step)
{
var pint = new google.maps.LatLng(
center.lat() + (radius/latC * Math.cos(i * Math.PI/180)),
center.lng() + (radius/lngC * Math.sin(i * Math.PI/180))
);
points.push(pint);
}
最佳答案
removeOverlay 是一个 v2 函数,与 GLatLng 一样:
map.removeOverlay(circle);
在 v3 中使用:
circle.setMap(null);
看起来您发布的代码实际上并未在实时页面中使用。您可能想向圆添加一个 radius_changed 监听器并更新其中的计算。
关于javascript - 在 Google Maps API v3 中获取更新的半径/区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636971/