2010-07-01 4 views
1

Je travaille actuellement sur une application qui extrait les emplacements Google Map d'un fichier XML et les charge dans la carte. Actuellement, la fonction Load() est attachée à la balise body. IOW, body onLoad = "load()" onunload = "GUnload()". Je travaille dans un environnement PHP donc j'ai créé un fichier qui fait le mapping et je veux l'inclure dans de nombreux endroits, pages, et juste lui passer des paramètres différents pour différentes cartes. Cela dit, je ne suis pas sûr de savoir comment lancer la fonction load() sauf sur l'étiquette body. Cela, bien sûr, est un problème si j'inclue simplement le fichier dans une page différente, car une deuxième balise body invaliderait mon HTML.Google Maps load() initiation

est ici la fonction de charge:

function load() { 
    if (isCompatible) { 
     // Create Map 
     map = new GMap2(document.getElementById("map")); 
     map.setCenter(new GLatLng(40, -90), 3); 

     // Add controls 
     map.addControl(new GLargeMapControl()); 
     map.addControl(new GMapTypeControl()); 

     resetPolygon(); 

     GDownloadUrl("<?php echo $XML; ?>", function(data) { 
var xml = GXml.parse(data); 
var markers_xml = xml.documentElement.getElementsByTagName("marker"); 
var bounds = new GLatLngBounds(); 
for (var i = 0; i < markers_xml.length; i++) { 
    var listid = markers_xml[i].getAttribute("lid"); 
    var voterid = markers_xml[i].getAttribute("voterid"); 
    var contacted = markers_xml[i].getAttribute("contacted"); 
var name = markers_xml[i].getAttribute("name"); 
var address = markers_xml[i].getAttribute("address"); 
var type = markers_xml[i].getAttribute("type"); 
var iconcolor = markers_xml[i].getAttribute("iconcolor"); 
var point = new GLatLng(parseFloat(markers_xml[i].getAttribute("lat")), 
     parseFloat(markers_xml[i].getAttribute("lng"))); 
if(contacted == '2'){ 
var thecolor = "#CCCCCC" 
}else{ 
var thecolor = iconcolor 
} 

var marker = createMarker(point, voterid, name, address, type, thecolor, listid); 
map.addOverlay(marker); 
bounds.extend(point); 
markers.push(marker); 
markers[i].voterid = voterid; 
markers[i].contacted = contacted; 
} 
map.setZoom(map.getBoundsZoomLevel(bounds)); 
map.setCenter(bounds.getCenter()); 

}); updatePoints();

} 
} 

Si vous désirez plus d'informations, faites le moi savoir. Toute aide est grandement appréciée.

Répondre

0

Je l'ai compris. Exécutez window.onload = charger avec Javascript après le chargement de l'élément.

Alors:

<div id="map" style="width: 98%; height: 525px; float:left; margin: 0 1% 0 1%"></div> 
<script type="text/javascript"> 

window.onload=load; 

function load() { 
    if (isCompatible) { 
     // Create Map 
     map = new GMap2(document.getElementById("map")); 
     map.setCenter(new GLatLng(40, -90), 3); 

     // Add controls 
     map.addControl(new GLargeMapControl()); 
     map.addControl(new GMapTypeControl()); 

     resetPolygon(); 

     GDownloadUrl("<?php echo $XML; ?>", function(data) { 
      var xml = GXml.parse(data); 
      var markers_xml = xml.documentElement.getElementsByTagName("marker"); 
      var bounds = new GLatLngBounds(); 
      for (var i = 0; i < markers_xml.length; i++) { 
      var listid = markers_xml[i].getAttribute("lid"); 
      var voterid = markers_xml[i].getAttribute("voterid"); 
      var contacted = markers_xml[i].getAttribute("contacted"); 
      var name = markers_xml[i].getAttribute("name"); 
      var address = markers_xml[i].getAttribute("address"); 
      var type = markers_xml[i].getAttribute("type"); 
      var iconcolor = markers_xml[i].getAttribute("iconcolor"); 
      var point = new GLatLng(parseFloat(markers_xml[i].getAttribute("lat")), 
            parseFloat(markers_xml[i].getAttribute("lng"))); 
      if(contacted == '2'){ 
       var thecolor = "#CCCCCC" 
      }else{ 
       var thecolor = iconcolor 
      } 

      var marker = createMarker(point, voterid, name, address, type, thecolor, listid); 
      map.addOverlay(marker); 
      bounds.extend(point); 
      markers.push(marker); 
      markers[i].voterid = voterid; 
      markers[i].contacted = contacted; 
      } 
      map.setZoom(map.getBoundsZoomLevel(bounds)); 
      map.setCenter(bounds.getCenter()); 
     }); 
     updatePoints(); 

    } 
} 

C'était la réponse à la question. Merci à tous ceux qui ont jeté un coup d'œil.

0

ne peut pas résister à faire des commentaires que vous avez un malformé-HTML problème de base si vous incluez un site avec une étiquette de corps à l'intérieur d'une page avec une autre balise body ... juste dire :)

+0

De toute évidence. C'est la raison pour laquelle j'ai posté la question. Ce commentaire n'a pas été utile. – jmorhardt

+0

Oui sry, je dois admettre que j'ai mal lu votre message. – Nicolas78