I found this file mappanslow.html last week on http://code.google.com/p/gmaps-samples-v3/source/browse/trunk/talks/io2011/map-usability/mappanslow.html?spec=svn265&r=265 and it was very useful for me to know how to get data from a json file. I m re-coding in V3 this map at present http://www.ville.rimouski.qc.ca/styles/carte/carte.jsp and my problem is that the user must be able to choose which data (from different json file) will be displayed by selecting what he wants in the select box under "Imprimer". mappaslow.html works fine with one json file but I tried to use the code for more than one json file and, if I list, per example, all my files like this:
<script type="text/javascript" src="file1.json"></script>
<script type="text/javascript" src="file2.json"></script>
<script type="text/javascript" src="file3.json"></script>
显示的数据总是来自上一个数据(文件3.json)。
有人知道该如何解决这个问题吗?
照片来自Mappaslow. html:
<script type="text/javascript" src="markers.json"></script>
<script type="text/javascript">
var map;
var timeout;
var m = [];
function init() {
map = new google.maps.Map(document.getElementById( map ), {
center: new google.maps.LatLng(44.85471, -93.24185),
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
});
google.maps.event.addListener(map, bounds_changed , function() {
clearMarkers();
if (timeout) {
window.clearTimeout(timeout);
}
timeout = window.setTimeout(function() {
addMarkers();
}, 500);
});
}
function addMarkers() {
var bounds = map.getBounds();
for (var i = 0; i < markers.length; i++) {
var pos = new google.maps.LatLng(markers[i].lat, markers[i].lng);
if (bounds.contains(pos)) {
var marker = new google.maps.Marker({
map: map,
position: pos
});
m.push(marker);
}
}
}
function clearMarkers() {
for (var i = 0, marker; marker = m[i]; i++) {
marker.setMap(null);
}
m = [];
}
google.maps.event.addDomListener(window, load , init);
</script>
这个例子中使用的json文件, 我的地图看起来是这样的:
var markers = [
{"lat":44.86065444748757, "lng":-93.26223975870084},
{"lat":44.84990797511595, "lng":-93.22287578639873}];
感谢您的建议@harllessdragon, 但我希望能够通过将选中的.json 命名为参数来指定正确的变量, 因为它们的结构相同。 例如 :
function addMarkers(jsonName) {
var rightJson = jsonName;
var bounds = map.getBounds();
for (var i = 0; i < rightJson.markers.length; i++) {
var pos = new google.maps.LatLng(rightJson.markers[i].lat, rightJson.markers[i].lng);
...
我尝试过很多方法,但总是导致语法错误或一些没有定义的东西。
别忘了目标是上面第二个链接的地图
@harlessdragon, 我终于找到了json文件正确的语法:
var markers = [{
"file1": [
{"lat": "number", "lng": "number"}, {"lat": "number", "lng": "number"},
]},{"file2": [
{"lat": "number", "lng": "number"}, {"lat": "number", "lng": "number"},
]},{"file3": [
{"lat": "number", "lng": "number"}, {"lat": number, lng: number"}
]
}];
我警告地看到,第一个循环承认了“文件”的正确编号。现在的问题是,任何事物都传入第二个循环:
for (var t = 0; t < markers[i].length; t++) {