gpt4 book ai didi

javascript - 从 JSON 渲染多个 Google Maps API V3 标记

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:50:15 26 4
gpt4 key购买 nike

尝试使用 Google Maps API V3 从 DB 的 PHP 输出的 JSON 中绘制多个标记。

map 在页面上初始化,但它不填充标记。浏览器警告“Resource interpreted as Other but transferred with MIME type undefined.”?

请提供进一步故障排除/调试的建议。

        <!--  load points from database into Locations JSON -->
$(document).ready(function () {
$.getJSON("map-service.php?action=listpoints", function(json) {

if (json.Locations.length > 0) {
for (i=0; i<json.Locations.length; i++) {
var location = json.Locations[i];
addMarker(location);
}
}
});

function addMarker(location) {
var point = new google.maps.LatLng(location.lat, location.lng);
var marker = new google.maps.Marker({
position:point,
map: map,
title: location.name
});
};


});

来自 map-service.php?action=listpoints 的验证示例 JSON 输出

{"Locations":[{"name":"Abco Mountain","lat":"49.424999","lng":"-125.855003"},{"name":"Adder Peak","lat":"49.248333","lng":"-125.320000"},{"name":"Alexandra Peak","lat":"49.738110","lng":"-125.489998"},{"name":"Argus Mountain","lat":"49.538612","lng":"-125.389999"},{"name":"Big Baldy Mountain","lat":"49.759998","lng":"-126.129997"}]}

最佳答案

我的问题源于我如何初始化原始 map 对象。它在我共享的代码中不可见……抱歉。

我这样做了:

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

并且应该这样做:

this.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

我在这篇文章中找到了解决方案 Adding markers after map created谢谢@yitwail!

关于javascript - 从 JSON 渲染多个 Google Maps API V3 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8669919/

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