gpt4 book ai didi

javascript - Google Maps API - 外部 JSON 中未显示标记

转载 作者:行者123 更新时间:2023-11-28 04:42:57 25 4
gpt4 key购买 nike

我正在尝试使用 Google map API 来显示许多多个 map 标记,数据位于外部 JSON 文件中的一系列数组中。

相关 HTML/Javascript 的片段

<div id="map_canvas" style="width:100%; height:100%"></div>
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(40.8039941, -77.863459),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
//get JSON data
$(document).ready(function() {
$.getJSON("crime_maps_test.json", function(json1) {
$.each(json1, function(key, data) {
var latLng = new google.maps.LatLng(data.lat, data.lng);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
title: data.Incident
});
marker.setMap(map);
});
});
});
</script>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=API_key&callback=initialize"></script>

crime_maps_test.json 片段(实际文件包含数百个数组)

[
{
"Incident": "PSU201701139"
,"Occurred": "3/25/17 23:25"
,"reported": "3/25/17 23:25"
,"nature of incident": "Resident Assistant reported the odor of marijuana, origin not located"
,"offenses": "Possession of Small Amount of Marijuana"
,"location": "Porter Hall"
,"disposition": "Open"
,"lat": 40.8008254
,"lng": -77.8587917
},
{
"Incident": "PSU201701136"
,"Occurred": "03/25/2017 9:25 PM to 9:30 PM"
,"reported": "3/25/17 21:31"
,"nature of incident": "Visitor observed highly intoxicated"
,"offenses": "Public Drunkenness"
,"location": "Bryce Jordan Center"
,"disposition": "Open"
,"lat": 40.8086228
,"lng": -77.8642905
},
{
"Incident": "PSU201701134"
,"Occurred": "03/25/2017 8:52 PM to 8:58 PM"
,"reported": "3/25/17 20:58"
,"nature of incident": "Resident Assistant reported the odor of marijuana, origin not located"
,"offenses": "Possession of Small Amount of Marijuana"
,"location": "Curtin Hall 5Th Floor"
,"disposition": "Open"
,"lat": 40.8051407
,"lng": -77.8633569
}
]

我在 GitHub 上托管该项目以避免交叉引用错误。本地图正在显示并且开发人员的工具控制台没有记录任何错误时,标记不会显示。我的代码中是否遗漏了一些阻止标记显示的内容?有人知道是什么导致或可能导致此问题吗?

最佳答案

事实证明,几个 JSON 条目存在问题。应该验证数据。

关于javascript - Google Maps API - 外部 JSON 中未显示标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43617182/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com