JDN Développeurs > Client Web > Utiliser l'API Google Maps

Utiliser l'API Google Maps > Utiliser XML, les sources

Précédente Aller plus loin... 
 

Le fichier XML que nous utilisons, qui doit impérativement être encodé en UTF-8 :

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <markers>
  3.   <marker lat="48.9" lng="2.3" info="Premier endroit"   details="Quelques informations supplémentaires" />
  4.   <marker lat="49"   lng="3"   info="Deuxième endroit"  details="Quelques informations supplémentaires" />
  5.   <marker lat="48.2" lng="2.7" info="Troisième endroit" details="Quelques informations supplémentaires" />
  6.   <marker lat="48.5" lng="2.4" info="Quatrieme endroit" details="Quelques informations supplémentaires" />
  7. </markers>

Notre fichier XHTML :

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  6.     <title>Utilisation de XML</title>
  7.     <script src="http://maps.google.com/maps?file=api&amp;v=2"
  8.             type="text/javascript"></script>
  9.     <script type="text/javascript">
  10.     //<![CDATA[
  11.  
  12.     function load() {
  13.       if (GBrowserIsCompatible()) {
  14.         var map = new GMap2(document.getElementById("map"));
  15.         map.addControl(new GSmallMapControl());
  16.         map.addControl(new GMapTypeControl());
  17.         map.setCenter(new GLatLng(48.7, 2.3), 8);
  18.  
  19.     function creerMarker(point, onglet1, onglet2) {
  20.       var marker = new GMarker(point);
  21.       var infoTabs = [
  22.         new GInfoWindowTab("Infos", onglet1),
  23.         new GInfoWindowTab("Détails", onglet2) 
  24.         ];
  25.       GEvent.addListener(marker, "click", function() {
  26.         marker.openInfoWindowTabsHtml(infoTabs);
  27.         });
  28.       return marker;
  29.       }
  30.  
  31.         GDownloadUrl("donnees.xml", function(data) {
  32.           var xml = GXml.parse(data);
  33.           var markers = xml.documentElement.getElementsByTagName("marker");
  34.           for (var i = 0; i < markers.length; i++) {
  35.             var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng")), 9);
  36.         var marker = creerMarker(point, markers[i].getAttribute("info"), markers[i].getAttribute("details") );
  37.             map.addOverlay(marker);
  38.           }
  39.         });
  40.       }
  41.     }
  42.  
  43.     //]]>
  44.     </script>
  45.   </head>
  46.  
  47.   <body onload="load()" onunload="GUnload()">
  48.     <div id="map" style="width: 600px; height: 200px"></div>
  49.   </body>
  50. </html>
Exemple de code HTML pour les infobulles :
  1. <marker lat="48.2" lng="2.7" info="Troisième endroit" details="Grâce à HTML, nous pouvons utiliser des images : &lt;br/&gt; &lt;img src='http://www.google.com/intl/fr_fr/images/maps_results_logo.gif'/&gt; " />
Précédente Retour au sommaire Page suivante 
Introduction | Pré-requis | Premières cartes | Utiliser XML | Aller plus loin...
 
|
Haut de page
Nos autres sites Société | Mentions légales | Contacts | Publicité | PA Emploi | Presse | Recrutement | Tous nos sites | Données personnelles
© Benchmark Group, 69/71 avenue Pierre Grenier. 92517 Boulogne Billancourt Cedex