【问题标题】:JSON feed on Google map is showing nothing谷歌地图上的 JSON 提要没有显示任何内容
【发布时间】:2016-10-15 02:35:45
【问题描述】:

我想要带有 JSON 提要的 Google 地图显示标记。但它不起作用。我找不到真正的问题。所以这是我的代码:

 var map;

// The JSON data
var json = [{  
   "OpperationErrorMsg":"",
   "IsSuccess":true,
   "ResultId":1000,
   "Timestamp":"2016-10-12T18:00:07.0232702Z",
   "Echo":null,
   "InSandbox":true,
   "DebugMessages":[  

   ],
   "MissingDetails":[  

   ],
   "ResponseData":[  
      {  
         "CallTimeLocal":"2016-10-10T06:28:48.7330000",
         "IncidentId":3374,
         "IncidentNumber":"HC2016004034",
         "CallTime":"2016-10-10T10:28:48.7330000",
         "ElapsedSeconds":0,
         "Location":"2712 E HANNA AVE",
         "BuildingName":null,
         "BuildingNumber":null,
         "NatureId":6743,
         "FirePriorityId":1,
         "CoordinateX":-82.429500000000,
         "CoordinateY":28.003389000000
      },
      {  
         "CallTimeLocal":"2016-10-10T11:28:36.7000000",
         "IncidentId":3382,
         "IncidentNumber":"HC2016004042",
         "CallTime":"2016-10-10T15:28:36.7000000",
         "ElapsedSeconds":0,
         "Location":"1220 APOLLO BEACH BLVD S",
         "BuildingName":"Apollo Beach Marina",
         "BuildingNumber":null,
         "NatureId":8035,
         "FirePriorityId":1,
         "CoordinateX":-82.422369000000,
         "CoordinateY":27.781254000000
      },
      {  
         "CallTimeLocal":"2016-10-10T14:29:59.8830000",
         "IncidentId":3387,
         "IncidentNumber":"HC2016004047",
         "CallTime":"2016-10-10T18:29:59.8830000",
         "ElapsedSeconds":0,
         "Location":"9600 SHELDONWOOD RD",
         "BuildingName":null,
         "BuildingNumber":null,
         "NatureId":6420,
         "FirePriorityId":12,
         "CoordinateX":-82.580530000000,
         "CoordinateY":28.034779000000
      },
      {  
         "CallTimeLocal":"2016-10-10T15:27:37.7270000",
         "IncidentId":3389,
         "IncidentNumber":"HC2016004049",
         "CallTime":"2016-10-10T19:27:37.7270000",
         "ElapsedSeconds":0,
         "Location":"4691 GALLAGHER RD",
         "BuildingName":"Strawberry Crest High School",
         "BuildingNumber":null,
         "NatureId":7873,
         "FirePriorityId":2,
         "CoordinateX":-82.236450000000,
         "CoordinateY":28.021233000000
      }
   ],
   "CurrentStatusData":null
}];



function initialize() {
  
  // Giving the map som options
  var mapOptions = {
    zoom: 6,
    center: new google.maps.LatLng(25.0,-80.0)
  };
  
  // Creating the map
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  
  
  // Looping through all the entries from the JSON data
  for(var i = 0; i < json.length; i++) {
    
    // Current object
    var obj = json[i];

    // Adding a new marker for the object
    var marker = new google.maps.Marker({
      position: new google.maps.LatLng(obj.CoordinateY,obj.CoordinateX),
      map: map,
        draggable: true,
    animation: google.maps.Animation.DROP,

      title: obj.BuildingName // this works, giving the marker a title with the correct title
    });
    
    // Adding a new info window for the object
    var clicker = addClicker(marker, obj.title);

  } // end loop
  
  
  // Adding a new click event listener for the object
  function addClicker(marker, content) {
    google.maps.event.addListener(marker, 'click', function() {
      
      if (infowindow) {infowindow.close();}
      infowindow = new google.maps.InfoWindow({content: content});
      infowindow.open(map, marker);
      
    });
  }
  
}

// Initialize the map
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
   <script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div id="map-canvas"></div>

我将在 HTML 页面上使用它。虽然 JSON 数据会自动更新,所以我无法更改 JSON 数组。

谢谢!

【问题讨论】:

  • 你能把剩下的代码贴出来吗?这似乎只是它的随机 sn-ps。例如您对 JSON 文件的请求在哪里发生?您对 google maps API 脚本的引用在哪里? (你有一个用于 jquery 2.1.1,但没有谷歌地图)你的 mapOptions 对象的其余部分在哪里?您在 window.load 事件中引用的初始化函数在哪里?等等
  • 嘿史蒂夫,我已经更新了帖子,这就是我现在所拥有的。我希望这会有所帮助。

标签: javascript jquery json google-maps


【解决方案1】:

我在您的代码中发现了这些问题:

  • 在创建每个标记的循环中,您正在循环json 数组。但是,这不是您的标记数据数组。 json[0] 是您的主要对象,json[0].ResponseData 是您需要循环的标记数组。所以我把那个值放在一个名为responses 的变量中,然后循环遍历它。我不知道 JSON 数据是否可以在其最外层数组中包含多个对象;如果是这样,您将需要一个外部循环来处理这些。现在我假设只有一个外部对象使用json[0] 寻址。
  • 当您调用addClicker 时,您传入了不存在的obj.title。大概你的意思是obj.BuildingName
  • 您的点击处理程序引用了一个名为infowindow 的变量,但在第一次点击时该变量不存在并导致错误。所以我将infowindow 声明为一个全局窗口。

那么,我是如何发现这些问题的呢?使用 JavaScript 调试器。通常我会在initialize() 函数的开头添加一个debugger; 语句,然后单步执行代码以查看发生了什么。这将表明主循环设置var obj = json[i]; 的位置没有得到预期值。

这在普通网页上效果很好,但在 SO 上的嵌入式 sn-p 中似乎效果不佳。 (调试器显示错误的源代码行。)因此,我开始添加 console.log(); 语句,看起来事情可能会出错,例如在 var obj = 分配之后立即添加 console.log( 'obj:', obj );

此外,根据标记的位置自动缩放和居中地图也很不错。我使用为每个标记扩展的LatLngBounds 添加了一些代码,然后在创建所有标记后添加了map.fitBounds()。如果你这样做,你不需要在第一次创建地图时明确缩放和居中,所以我删除了这些。 (否则地图会显示在一个位置,然后重新定位。)

fitBounds() 的一个警告:如果有 no 标记,则地图根本不会显示。要处理这种情况,您需要检查responses.length 为零的情况,并使用默认值调用map.setZoom()map.setCenter()

我用////标记了更改的行,以便于查找:

 var map, infowindow; ////

// The JSON data
var json = [{  
   "OpperationErrorMsg":"",
   "IsSuccess":true,
   "ResultId":1000,
   "Timestamp":"2016-10-12T18:00:07.0232702Z",
   "Echo":null,
   "InSandbox":true,
   "DebugMessages":[  

   ],
   "MissingDetails":[  

   ],
   "ResponseData":[  
      {  
         "CallTimeLocal":"2016-10-10T06:28:48.7330000",
         "IncidentId":3374,
         "IncidentNumber":"HC2016004034",
         "CallTime":"2016-10-10T10:28:48.7330000",
         "ElapsedSeconds":0,
         "Location":"2712 E HANNA AVE",
         "BuildingName":null,
         "BuildingNumber":null,
         "NatureId":6743,
         "FirePriorityId":1,
         "CoordinateX":-82.429500000000,
         "CoordinateY":28.003389000000
      },
      {  
         "CallTimeLocal":"2016-10-10T11:28:36.7000000",
         "IncidentId":3382,
         "IncidentNumber":"HC2016004042",
         "CallTime":"2016-10-10T15:28:36.7000000",
         "ElapsedSeconds":0,
         "Location":"1220 APOLLO BEACH BLVD S",
         "BuildingName":"Apollo Beach Marina",
         "BuildingNumber":null,
         "NatureId":8035,
         "FirePriorityId":1,
         "CoordinateX":-82.422369000000,
         "CoordinateY":27.781254000000
      },
      {  
         "CallTimeLocal":"2016-10-10T14:29:59.8830000",
         "IncidentId":3387,
         "IncidentNumber":"HC2016004047",
         "CallTime":"2016-10-10T18:29:59.8830000",
         "ElapsedSeconds":0,
         "Location":"9600 SHELDONWOOD RD",
         "BuildingName":null,
         "BuildingNumber":null,
         "NatureId":6420,
         "FirePriorityId":12,
         "CoordinateX":-82.580530000000,
         "CoordinateY":28.034779000000
      },
      {  
         "CallTimeLocal":"2016-10-10T15:27:37.7270000",
         "IncidentId":3389,
         "IncidentNumber":"HC2016004049",
         "CallTime":"2016-10-10T19:27:37.7270000",
         "ElapsedSeconds":0,
         "Location":"4691 GALLAGHER RD",
         "BuildingName":"Strawberry Crest High School",
         "BuildingNumber":null,
         "NatureId":7873,
         "FirePriorityId":2,
         "CoordinateX":-82.236450000000,
         "CoordinateY":28.021233000000
      }
   ],
   "CurrentStatusData":null
}];

function initialize() {
  
  // Giving the map som options
  var mapOptions = {
    ////
  };
  
  // Creating the map
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  
  var bounds = new google.maps.LatLngBounds(); ////
  
  // Looping through all the entries from the JSON data
  var responses = json[0].ResponseData; ////
  for(var i = 0; i < responses.length; i++) { ////
    
    // Current object
    var obj = responses[i]; ////

    // Adding a new marker for the object
    var position =
      new google.maps.LatLng( obj.CoordinateY, obj.CoordinateX ); ////
    bounds.extend( position ); ////
    var marker = new google.maps.Marker({
      position: position, ////
      map: map,
      draggable: true,
      animation: google.maps.Animation.DROP,
      title: obj.BuildingName
    });
    
    // Adding a new info window for the object
    var clicker = addClicker(marker, obj.BuildingName); ////

  } // end loop
  
  map.fitBounds( bounds ); ////
  
  // Adding a new click event listener for the object
  function addClicker(marker, content) {
    google.maps.event.addListener(marker, 'click', function() {
      
      if (infowindow) {infowindow.close();}
      infowindow = new google.maps.InfoWindow({content: content});
      infowindow.open(map, marker);
      
    });
  }
  
}

// Initialize the map
google.maps.event.addDomListener(window, 'load', initialize);
html, body, #map-canvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
   <script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<div id="map-canvas"></div>

【讨论】:

  • 这是一个比我发布的更好的答案。删除了我的答案,我遵从这个答案。感谢bounds
  • @Steve:谢谢你的好话。是的,map.fitBounds() 是我最喜欢的技巧之一——它使自动缩放和居中地图变得如此容易。
  • 今天学到了新东西!不知道在 API 中是否可用。我一直在做一个更难的方式。现在我需要用我的新技巧重构一些代码。 ;)
  • @Michael:谢谢,但我还有一个问题要问。如果我想从外部服务器加载 Json 文件,那么我将如何加载变量?我已经用 .getjson 试过了,但我卡住了。
  • 使用$.getJSON()提供的回调函数。回调函数是您可以访问 JSON 数据的地方。 $.getJSON()doc page 上有一些例子。页面顶部附近的代码 sn-p 就是一个很好的例子:看看它是如何引用成功回调中的data 变量的?那是从服务器下载的 JSON 数据,这是您初始化标记的地方。如果您仍有问题,也许可以发布一个新问题,但请随时在此处@我并提供指向新问题的链接。
猜你喜欢
  • 2017-05-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多