【问题标题】:different data in different json file. How to load them不同的json文件中的不同数据。如何加载它们
【发布时间】:2015-09-13 07:13:45
【问题描述】:

我有两个不同的 json 文件(marker.jsonmarker.json0),它们包含不同的坐标(纬度和经度),我需要通过重命名来加载它们 .json0 到 .json (显然以前的 .json 现在是 .json0),以便包含不同的数据在以前的 .json0 中,现在 .json 在处理导致标记和信息窗口显示在地图上的 ajax 请求的 javascript 文件中进行解析。

  1. 这是处理异步加载的 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 错误:

  1. TypeError:标记未定义
  2. ReferenceError: searchAddress is not defined(此错误仅在我单击“cerca”按钮时发生,只要单击该按钮就会触发onclick事件


  1. 这是第一个包含纬度、经度、标题和内容的 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"
},
........
]
  1. 这是包含新信息的第二个 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


【解决方案1】:

如果我正确理解了您的问题,这里有解决方法,希望能解决您的问题。似乎 json 属性标题
纬度 经度 内容
图标
坐标
类型
在这两个文件中,纬度和经度值会有所不同。在这种情况下,为什么我们不能使用具有这些属性的 pojo 并使用 Gson api 将 json 编组到这个 pojo。所以现在 json1 将在 pojo object1 中,而 json2 将在 pojo object2 中。将这两个对象添加到列表中,并将列表添加到 JsonParser。这将通过合并两个 json 文件数据再次生成一个 json。现在 JsonParser 生成的数据发送到 java 脚本文件。 希望很清楚。

【讨论】:

  • 能否请您将您的示例添加到代码的 sn-p 中,以便它比现在更清晰,请不要被这个请求所困扰...您刚刚编写的内容将其放入代码示例你能做到吗?
【解决方案2】:

如果你不重命名任何东西会简单得多。由于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) 重用相同的代码。

【讨论】:

  • markers和markers.concat我想需要在代码开头输入
  • 没有。在代码的最后,由于在函数之前列出它们意味着该函数是undefined。它们都必须位于代码的最结束
  • 然后,标记可以作为参数传递给解析标记的函数。
  • 好的,我会尝试,但是我预计我的问题没有收到,因此被否决
  • markers 和markers.concat 是未定义的,也许他们需要被声明为我想的变量
猜你喜欢
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-07
  • 1970-01-01
相关资源
最近更新 更多