gpt4 book ai didi

javascript - 从外部 json 向谷歌地图添加标记?

转载 作者:行者123 更新时间:2023-11-30 10:21:05 25 4
gpt4 key购买 nike

我必须向谷歌地图添加多个标记,但数据位于外部 json 文件中。

目前我正在尝试使用 jquery getJSON 运行它。但它根本不起作用,并且控制台没有返回任何错误!

    google.maps.event.addDomListener(window, 'load', initialize);



function initialize() {

var map_canvas = document.getElementById('map1');


var map_options = {
center: new google.maps.LatLng(44.5403, -78.5463),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(map_canvas, map_options);



$.getJSON('map_points.json', function(data) {
$.each( data.points, function(i, value) {
var myLatlng = new google.maps.LatLng(value.lat, value.lon);
alert(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Hello World!"
});

});


});


} //End initialize()

还有 Json

{

"points":[
{"id":1,"lat":44.5403,"lon":-79.5463},
{"id":2,"lat":45.5403,"lon":-78.5463},
{"id":3,"lat":45.5403,"lon":-76.5463},
{"id":4,"lat":45.5403,"lon":-77.5463}
]


}

最佳答案

我刚刚测试了下面的代码并且它正在运行。试试看:

<html>
<head>
<title>Eg. Google Maps Marker using External JSON</title>
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src = "http://maps.google.com/maps/api/js?sensor=false">

</script>
<script type="text/javascript">
function initialize() {

var mapOptions = {
center: new google.maps.LatLng(44.5403, -78.5463),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};

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

$.getJSON('map_points.json', function(data) {
$.each( data.points, function(i, value) {

var myLatlng = new google.maps.LatLng(value.lat, value.lon);
alert(myLatlng);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: "text "+value.lon
});

});
});


}

</script>
</head>
<body onload="initialize()">
<form id="form1" runat="server">
<div id="map_canvas" style="width: 500px; height: 400px"></div>
</form>
</body>
</html>

关于javascript - 从外部 json 向谷歌地图添加标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21401774/

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