我有两个不同的json文件( marker.json 和 marker.json0 ),它们包含不同的坐标(纬度和经度),我需要通过重命名加载它们< em> .json0 到 .json (显然前者 .json 现在是 .json0 )以便包含不同的数据在前 .json0 现在 .json 在javascript文件中解析,该文件处理导致标记和infowindow显示在地图上的ajax请求。
这是处理异步加载的javascript文件 json数组。此外,有两个功能搜索和删除。 当用户单击“cerca”或时,它们都会被触发 “cancella”按钮,但他们的目的是不同的。 searchAddress解析 循环内的json数组,以便标记和 当removeAddress从地图中删除它们时,infowindow是可见的, 现在我需要再次单击“cerca”按钮重新创建标记和 infowindow这次属于我的新json文件 webserver我将.json重命名为js文件中使用的扩展名。最后一个编辑构建了一个处理ajax请求的函数,在代码结束时我加载了两个json文件但仍然标记在同一个coords上。 我删除了几乎所有的全局变量,并尝试使用参数来定义函数内部的变量。尚未找到解决方案
编辑js文件
var map; function initialize() { var mapOptions = { zoom: 16, center: new google.maps.LatLng(41.898055, 12.515112) }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); } google.maps.event.addDomListener(window, 'load', initialize); function Get(url) { var xmlhttp = new XMLHttpRequest(); console.log(url); arr; xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { arr = JSON.parse(xmlhttp.responseText); } } xmlhttp.open("GET", url, true); xmlhttp.send(); function Markers(arr) { var infowindow = []; var marker = []; function searchAddress(){ for(var i = 0; i < arr.length; i++) { console.log(arr); (function(i){ //new line infowindow[i] = new google.maps.InfoWindow({ title: arr[i].title, content: arr[i].content }); marker[i] = new google.maps.Marker({ title: arr[i].title, icon: arr[i].icon, size: arr[i].size, coords: arr[i].coords, type: arr[i].type, draggable: false, //map: map, animation: google.maps.Animation.DROP, //animation: google.maps.Animation.BOUNCE, position: new google.maps.LatLng(arr[i].latitude,arr[i].longitude) }); marker[i].setMap(map) google.maps.event.addListener(marker[i], 'click', function() { infowindow[i].open(map,this); }); })(i); //new line } } // Sets the map on all markers in the array. function setAllMap(map) { for (var i = 0; i < marker.length; i++) { marker[i].setMap(map); } } function removeAddress(){ setAllMap(null); prevArr = arr.slice(); } } markers = Get("http://89.97.214.162/accessibilita json/marker_json1.json"); markers.concat(Get("http://89.97.214.162/accessibilita/json/marker_json2.json"));
编辑代码后出现了新的javascript错误:
onclick
事件这是第一个包含在js数组中解析的纬度,经度,标题和内容的json数组,以便地图能够检索它们并显示所需的信息(.json)
[
{
"title": "Paolo",
"latitude": 41.897115,
"longitude": 12.513300,
"content": "Cooperativa fornitrice di servizi sociali,<br/> Viale Eleonora D'Arborea 12<br/> 00162 Roma<br/> <a href='http://www.prassiericerca.com' target='_blank'>Prassi e Ricerca</a>",
"icon": "img/orange-dot.png",
"coords": "1, 1, 1, 20, 18, 20, 18 , 1",
"type": "poly"
},
{
"title": "Galasso",
"latitude": 41.897379,
"longitude": 12.513272,
"content": "Penelope e altri servizi <a href='http://www.borghiartistici.com' target='_blank'>Borghi Artistici S.r.l.</a>",
"icon": "img/green-dot.png",
"coords": "1, 1, 1, 20, 18, 20, 18 , 1",
"type": "poly"
},
........
]
这是包含新信息的第二个json数组,每当该文件从.json1重命名为.json,另一个从.json重命名为.json1时,应该加载该信息 json文件加载在js文件中处理
[
{
"title": "Bar dei Pini",
"latitude": 41.897115,
"longitude": 12.513300,
"content": "Specialita Gelato Artigianale<br/> Viale Eleonora D'Arborea 12<br/> 00162 Roma<br/> <a href='http://www.prassiericerca.com' target='_blank'>Prassi e Ricerca</a>",
"icon": "img/pink-dot.png",
"coords": "1, 1, 1, 20, 18, 20, 18 , 1",
"type": "poly"
},
{
"title": "Alimentari San Lorenzo",
"latitude": 41.897379,
"longitude": 12.513272,
"content": "Specialita Calabresi<a href='http://www.borghiartistici.com' target='_blank'>Borghi Artistici S.r.l.</a>",
"icon": "img/green-dot.png",
"coords": "1, 1, 1, 20, 18, 20, 18 , 1",
"type": "poly"
},
........
]
事实上,不需要重新加载页面,因为两个json文件都被重命名,只要两个中的至少一个是.json
答案 0 :(得分:0)
如果我正确地理解了你的问题,这里有解决方法,希望能解决你的问题。似乎是json属性标题
纬度
经度
内容
图标
COORDS
键入
两个文件都相同,其中lati和经度值会有所不同。在这种情况下,为什么我们不能使用这些属性的pojo并使用Gson api将json编组到这个pojo。所以现在json1将在pojo object1中,而json2将在pojo object2中。将这两个对象添加到列表中,并将列表添加到JsonParser。这将通过使用两个json文件数据来再次生成json。现在JsonParser生成的数据发送到java脚本文件。
希望很清楚。
答案 1 :(得分:0)
如果你没有重命名,那就简单多了。由于XMLHttpRequest
是客户端操作,因此它无权访问服务器上的重命名文件,例如json1.json
。什么阻止您将获取JSON的所有代码转换为带有参数作为URL的函数?
function Get(url){
var xmlhttp = new XMLHttpRequest();
console.log(url);
var arr;
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
arr = JSON.parse(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
return arr;
}
这样,您就可以获得所需的所有文件。所以你可以这样做:
markers = Get("http://www.example.com/json1.json");
markers.concat(Get("http://www.example.com/json2.json"));
或类似的东西。
然后可以对您的标记代码执行类似的操作。放入函数后,您可以将markers
变量作为参数提交给该函数,它将在该映射中显示它们。
基本上,尝试将代码分解为函数a),以便它可以更容易地流动,并且b)重用相同的代码。