我可以使用以下方法在 Google 地图上绘制点列表:
(function() {
window.onload = function() {
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(41.6, -0.88),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
// Creating the JSON data
var json = [
{
"nombre": "NAME1",
"lat": "41.653",
"long": "-0.907922",
},
{
"nombre": "NAME2",
"lat": "41.6693",
"long": "-0.891317",
},
{
"nombre": "NAME3",
"lat": "41.6502",
"long": "-0.875212",
}
];
var infoWindow = new google.maps.InfoWindow();
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.long);
// Creating a marker and putting it on the map
//var iconBase = 'https://dea-srl.net/domenico/traking/js/';
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.nombre,
icon: iconBase + 'schools_maps.png'
});
(function(marker, data) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.nombre);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
})();
它运行良好,但我需要从生成的 json 字符串格式中读取我的位置,如下所示:
{
"locations": [
{
"nombre": "Name1",
"lat": "41.6504",
"long": "-0.879137",
},
{
"nombre": "NAME2",
"lat": "41.6405",
"long": "-0.893266",
}
]
}
此 Json 是从 php 脚本生成的,可通过以下网址获得:http://myscript.php。
我的问题是,如何从 php 脚本中获取 JSON 并将其放入 JavaScript 变量中?