【发布时间】:2015-09-13 07:13:45
【问题描述】:
我有两个不同的 json 文件(marker.json 和 marker.json0),它们包含不同的坐标(纬度和经度),我需要通过重命名来加载它们 .json0 到 .json (显然以前的 .json 现在是 .json0),以便包含不同的数据在以前的 .json0 中,现在 .json 在处理导致标记和信息窗口显示在地图上的 ajax 请求的 javascript 文件中进行解析。
这是处理异步加载的 javascript 文件 json 数组。此外,还有搜索和删除两个功能。 每当用户单击“cerca”或 “取消”按钮,但它们的用途不同。搜索地址解析 循环内的 json 数组,以便标记和 信息窗口可见,而 removeAddress 从地图中删除它们, 现在我需要再次单击“cerca”按钮来重新创建标记和 这次属于我的新 json 文件的 infowindow webserver 我将 js 文件中使用的扩展名重命名为 .json。最后一次编辑构建了一个处理 ajax 请求的函数,在代码的最后我加载了两个 json 文件,但标记仍然在相同的坐标上。 我已经删除了几乎所有的全局变量,并尝试使用参数来定义函数内部的变量。还没找到解决办法
编辑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 错误:
- TypeError:标记未定义
- ReferenceError: searchAddress is not defined(此错误仅在我单击“cerca”按钮时发生,只要单击该按钮就会触发
onclick事件
这是第一个包含纬度、经度、标题和内容的 json 数组,这些数组在 js 数组中解析,以便地图能够检索它们并显示所需的信息 (.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 数组 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,两个 json 文件就会被重命名
【问题讨论】:
-
如此有趣的文本格式...
-
什么意思?但是我已经批准了你的编辑我的朋友
-
只是一个建议-对于一个会吸引大量答案的问题来说,这可能是一种太长的格式,如果可以的话,请尝试将其分解为一个更小更简洁的示例/查询,包括最少的代码量
-
我的意思是“如此有趣的文本格式”。没有讽刺或别的什么。例如,您对纯文本使用块引用。这很不寻常:)而且,是的,编辑建议不是我的:)
-
我取出了一些不重要的材料(比如 HTML 和 CSS,以及重复的 JSON)
标签: javascript html json css google-maps-api-3