0

我正在使用 Google Map API v3,并且我的代码通过 jQuery 从 JSON 文件调用。

$.getJSON("/temp/google_maps/json/google_map.json", {}, function(data){
        $.each(data.location, function(i, item){
            $("#markers").append('<li><a href="#" rel="' + i + '">' + item.title + '</a></li>');
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(item.lat, item.lng),
                map: map,
                title: item.title
            });
            arrMarkers[i] = marker;
            var infowindow = new google.maps.InfoWindow({
                content: "<h3>"+ item.title +"</h3><p>"+ item.description +"</p>"
            });
            arrInfoWindows[i] = infowindow;
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.open(map, marker);
            });
        });
    });

JSON 文件如下所示:

{
"location": [
    {
        "title": "Castillo San Felipe del Morro - San Juan",
        "description": "Fort San Felipe del Morro —or El Castillo San Felipe del Morro in Spanish— is a sixteenth-century citadel which lies on the northwestern-most point of the islet of San Juan, Puerto Rico. Named in honor of King Philip II of Spain, the fort, also referred to as \"El Morro\" or \"promontory\", was designed to guard the entrance to San Juan bay, and defend the city of San Juan from seaborne enemies. <a href=\"http://en.wikipedia.org/wiki/Fort_San_Felipe_del_Morro\">More Info</a>",
        "lat": 18.470186,
        "lng": -66.122096
    },
    {
        "title": "Playa de Jobos - Isabela",
        "description": "Beautiful beach, great for surfing and soaking up the sun.",
        "lat": 18.51379572782087,
        "lng": -67.07676887512207
    },
    {
        "title": "Radiotelescopio de Arecibo - Arecibo",
        "description": "The Arecibo Observatory is a very sensitive radio telescope located approximately 9 miles (14 km) south-southwest from the city of Arecibo in Puerto Rico. It is operated by Cornell University under cooperative agreement with the National Science Foundation. The observatory works as the National Astronomy and Ionosphere Center (NAIC) although both names are officially used to refer to it. NAIC more properly refers to the organization that runs both the observatory and associated offices at Cornell University. <a href=\"http://en.wikipedia.org/wiki/Arecibo_Observatory\">More Info</a>",
        "lat": 18.344179271158357,
        "lng": -66.75267219543457
    }
]
}

正如我所说的效果很好,但现在我必须调整脚本以从以下文件中提取:

{
    "TYPE" : ["location"], "DATA" :[
        {

相反,如果前一个:

{
    "location": [
        {

如何更改我的 jQuery 以调用此数据集?任何帮助将不胜感激。谢谢你。

4

1 回答 1

1

除非我弄错了,否则您只是更改data.locationdata.data例如:

$.getJSON("/temp/google_maps/json/google_map.json", {}, function(data){
    $.each(data.DATA, function(i, item){
        $("#markers").append('<li><a href="#" rel="' + i + '">' + item.title + '</a></li>');
        var marker = new google.maps.Marker({
            position: new google.maps.LatLng(item.lat, item.lng),
            map: map,
            title: item.title
        });
        arrMarkers[i] = marker;
        var infowindow = new google.maps.InfoWindow({
            content: "<h3>"+ item.title +"</h3><p>"+ item.description +"</p>"
        });
        arrInfoWindows[i] = infowindow;
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.open(map, marker);
        });
    });
});
于 2012-04-10T18:47:32.667 回答