谷歌地图Javascript API发布绘制多个圆
Google Maps Javascript API Issue Drawing Multiple Circles
所以我试图用以下代码绘制多个圆心不同、半径不同的圆:
var citymap = {
chicago: {
loc : "Chicago, IL",
center: {lat: 40, lng: -70},
shootings: 140000
},
newyork: {
loc : "New York, NY",
center: {lat: 40, lng: -70},
shootings: 80000
},
losangeles: {
loc : "Los Angeles, CA",
center: {lat: 40, lng: -70},
shootings: 40000
},
};
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 39.14, lng: -98.1},
zoom: 5
});
for (var city in citymap) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': citymap[city].loc},
function(results, status) {
citymap[city].center = {
lat : results[0].geometry.location.lat(),
lng : results[0].geometry.location.lng()
}
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.5,
map: map,
center: citymap[city].center,
radius: citymap[city].shootings
});
});
}
我觉得这应该有效,但所有的圆都是用相同的半径画的。如果我把cityCircle放在地理编码器函数之外,它会绘制半径不同但都在同一点上的圆。有人知道这里的问题是什么吗?
地理编码器是异步的。当回调函数运行时,循环已经完成,city
保留在最后一个值,因此所有圆都获得最后一个条目的半径。
一个修复方法是使用函数闭包:
function createCircleFromGeocode(city) {
geocoder.geocode({
'address': citymap[city].loc
}, function(results, status) {
citymap[city].center = {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
}
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.5,
map: map,
center: citymap[city].center,
radius: citymap[city].shootings
});
});
}
概念验证小提琴
代码片段:
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 39.14,
lng: -98.1
},
zoom: 3
});
for (var city in citymap) {
createCircleFromGeocode(city);
}
}
function createCircleFromGeocode(city) {
geocoder.geocode({
'address': citymap[city].loc
}, function(results, status) {
citymap[city].center = {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
}
// Add the circle for this city to the map.
var cityCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.5,
map: map,
center: citymap[city].center,
radius: citymap[city].shootings
});
});
}
var geocoder = new google.maps.Geocoder();
var citymap = {
chicago: {
loc: "Chicago, IL",
center: {
lat: 40,
lng: -70
},
shootings: 140000
},
newyork: {
loc: "New York, NY",
center: {
lat: 40,
lng: -80
},
shootings: 80000
},
losangeles: {
loc: "Los Angeles, CA",
center: {
lat: 40,
lng: -100
},
shootings: 40000
},
};
var map;
google.maps.event.addDomListener(window, 'load', initMap);
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>
相关文章:
- 有没有一个javascript图形绘制库可以进行气球树布局
- 绘制(重新加载)具有cors=“”的图像;匿名的“;在画布上(javascript)
- 如何使用Javascript在画布上绘制
- 用d3和numericjs在javascript中绘制错误椭圆
- Javascript:延迟在html5画布上循环绘制
- 谷歌地图API在Javascript中,绘制圆的半径
- Javascript和Canvas绘制中间有孔的圆
- Javascript绘制画布内存泄漏
- 我可以将javascript绘制函数更改为图像吗
- 如何在重新绘制画布时防止形状的痕迹-Javascript
- 如何在javascript中在图像上绘制图像(没有CSS和HTML)
- 用于绘制工作流图的JavaScript框架
- 在画布上绘制不透明度(行中的点)javascript
- 如何在不使用外部库的情况下使用 JavaScript 绘制折线图
- 我需要最快、最简单的方法来在 Javascript 中绘制数据
- 精灵不在画布上绘制(Javascript)
- 图片不是第一次绘制javascript
- 用于分析和绘制Javascript函数之间依赖关系的工具
- Django不能读取CSV文件来绘制javascript和HTML模板中的highchart
- 如何绘制javascript数组到jqplot