【发布时间】:2018-08-04 04:48:22
【问题描述】:
我正在尝试从 API 加载数据,然后使用圆圈显示它。我能够用数据点而不是圆圈创建标记。我正在关注this example here from Google's documentation。
我期望在for loop 中发生,使用center: new google.maps.LatLng(well.location.latitude, well.location.longitude) 就足以创建中心点。但是,这似乎不起作用。其他都和例子一样(后面会修改)。
我预计这会起作用,因为在前面的示例中,我可以使用 $.each 来显示使用 field.location.latitude, field.location.longitude 的标记,这本质上是相同的(或者我认为是这样)。
我不能像使用标记那样在$.getJSON 函数中画圈吗?是否发生“不同步”?我仍在尝试学习如何处理异步事件。
HTML:
<head>
<script src="http://maps.googleapis.com/maps/api/js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<div id="map"></div>
</body>
CSS:
#map {
border: 1px solid black;
margin: 0 auto;
width: 500px;
height: 300px;
}
JavaScript
var map;
var mapProp;
var url;
var marker;
var markers = [];
var infoWindow;
var wellCircle;
function initMap() {
mapProp = {
center: new google.maps.LatLng(39.0, -105.782067),
zoom: 6,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
map = new google.maps.Map(document.getElementById("map"), mapProp);
infoWindow = new google.maps.InfoWindow({
content: "hello world"
});
};
function addMarker(lat, lng) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map
});
markers.push(marker);
//console.log(markers);
};
$(document).ready(function() {
url = 'https://data.colorado.gov/resource/hfwh-wsgi.json?&$limit=500';
initMap();
$.getJSON(url, function(data) {
//console.log(data);
for (var i = 0; i < data.length; i++) {
//console.log(data[i].location.latitude + ", " + data[i].location.longitude);
};
$.each(data, function(i, field) {
addMarker(field.location.latitude, field.location.longitude);
});
for (var well in data) {
wellCircle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
center: new google.maps.LatLng(well.location.latitude,
well.location.longitude),
radius: 100000
});
};
});
});
【问题讨论】:
标签: javascript google-maps getjson geometry