2012-04-10 3 views
0

Ich arbeite mit Google Map API v3 und lasse meinen Code von einer JSON-Datei über jQuery aufrufen.Aufruf des Standortdatentyps innerhalb der JSON-Datei

$.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); 
      }); 
     }); 
    }); 

Die JSON-Datei wie folgt aussieht:

{ 
"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 
    } 
] 
} 

Wie gesagt funktioniert super, aber jetzt muss ich das Skript anpassen aus einer Datei zu ziehen, die mit Beginn des Einsatzes:

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

Stattdessen wenn die vorherige:

{ 
    "location": [ 
     { 

Wie ändere ich meine jQuery, um diesen Datensatz aufzurufen? Jede Hilfe würde sehr geschätzt werden. Vielen Dank.

Antwort

1

Wenn ich mich nicht irre Sie ändern sich nur data.location zu data.data z.B .:

$.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); 
     }); 
    }); 
}); 
+0

Das ist, was ich auch gedacht, aber keine, die nicht traurig funktionierte. –

+0

ist so einfach wie Standort-> DATEN zu ändern. – Nix

+0

Ich muss das irgendwo durcheinander gebracht haben. Du hast recht, es ist so einfach. Danke für Ihre Hilfe. –

Verwandte Themen