L'API Google Maps permet de superposer des fichiers KML sur une carte. Et d'afficher une mini-carte dans une infobulle (ce qui n'a rien à voir avec l'affichage du KML mais qui peut être intéressant).
L'appel à un fichier KML (dans le cas présent un fichier KML bien connu des SIGMA-iens (-ieux, -iois, au choix)) se fait grâce à la classe GGeoXml : var kml = new GGeoXml("http://88.191.39.115/fabien/geotribu/kml/route.kml"); Et l'affichage grâce à la méthode addOverlay() déjà utilisée précédemment : map.addOverlay(kml);
Créons tout d'abord un marqueur et ajoutons-le à la carte : var marker1 = new GMarker(new GLatLng(43.57769664771851, 1.402821992034912)); map.addOverlay(marker1); Puis gérons l'événement lors d'un clic sur celui-ci : GEvent.addListener(marker1, 'click', function() { marker1.showMapBlowup({zoomLevel:18,mapType:G_SATELLITE_MAP}); });
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><head><title> [Google Maps] 8. Superposer un fichier .kml et enrichir les info-bulles
</title><styletype="text/css">html{overflow:hidden;height:100%;}body{height:100%;margin:0;}#map{width:100%;height:100%;}</style><linkrel="icon"type="image/png"href="./favicon.png"/><scriptsrc="http://maps.google.com/maps?file=api&v=2.x&key=votre_clé_ici"type="text/javascript"></script><scripttype="text/javascript">functioninitialize(){if(GBrowserIsCompatible()){varmap=newGMap2(document.getElementById('map'));functionZoomControl(){}ZoomControl.prototype=newGControl();ZoomControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varzoomInDiv=document.createElement("div");this.setButtonStyle_(zoomInDiv);container.appendChild(zoomInDiv);zoomInDiv.appendChild(document.createTextNode("Zoom In"));zoomInDiv.innerHTML='<img src="./icons/32x32/1.png">';GEvent.addDomListener(zoomInDiv,"click",function(){map.zoomIn();});varzoomOutDiv=document.createElement("div");this.setButtonStyle_(zoomOutDiv);container.appendChild(zoomOutDiv);zoomOutDiv.innerHTML='<img src="./icons/32x32/2.png">';GEvent.addDomListener(zoomOutDiv,"click",function(){map.zoomOut();});map.getContainer().appendChild(container);returncontainer;}ZoomControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_TOP_LEFT,newGSize(5,5));}ZoomControl.prototype.setButtonStyle_=function(button){button.style.cursor="pointer";}functionPanUpControl(){}PanUpControl.prototype=newGControl();PanUpControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varpanUp=document.createElement("div");this.setButtonStyle_(panUp);container.appendChild(panUp);panUp.innerHTML='<img src="./icons/32x32/9.png">';GEvent.addDomListener(panUp,"click",function(){map.panDirection(0,1);});map.getContainer().appendChild(container);returncontainer;}PanUpControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_TOP_RIGHT,newGSize(map.getSize().width/2-16,0));}PanUpControl.prototype.setButtonStyle_=function(button){button.style.cursor="pointer";}functionPanDownControl(){}PanDownControl.prototype=newGControl();PanDownControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varpanDown=document.createElement("div");this.setButtonStyle_(panDown);container.appendChild(panDown);panDown.innerHTML='<img src="./icons/32x32/10.png">';GEvent.addDomListener(panDown,"click",function(){map.panDirection(0,-1);});map.getContainer().appendChild(container);returncontainer;}PanDownControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_BOTTOM_RIGHT,newGSize(map.getSize().width/2-16,0));}PanDownControl.prototype.setButtonStyle_=function(button){button.style.cursor="pointer";}functionPanRightControl(){}PanRightControl.prototype=newGControl();PanRightControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varpanRight=document.createElement("div");this.setButtonStyle_(panRight);container.appendChild(panRight);panRight.innerHTML='<img src="./icons/32x32/7.png">';GEvent.addDomListener(panRight,"click",function(){map.panDirection(-1,0);});map.getContainer().appendChild(container);returncontainer;}PanRightControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_TOP_RIGHT,newGSize(0,map.getSize().height/2-16));}PanRightControl.prototype.setButtonStyle_=function(button){button.style.cursor="pointer";}functionPanLeftControl(){}PanLeftControl.prototype=newGControl();PanLeftControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varpanLeft=document.createElement("div");this.setButtonStyle_(panLeft);container.appendChild(panLeft);panLeft.innerHTML='<img src="./icons/32x32/8.png">';GEvent.addDomListener(panLeft,"click",function(){map.panDirection(1,0);});map.getContainer().appendChild(container);returncontainer;}PanLeftControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_TOP_LEFT,newGSize(0,map.getSize().height/2-16));}PanLeftControl.prototype.setButtonStyle_=function(button){button.style.cursor="pointer";}functionCustomMapControl(){}CustomMapControl.prototype=newGControl();CustomMapControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varCustomMapPlan=document.createElement("div");this.setButtonStyle_(CustomMapPlan);container.appendChild(CustomMapPlan);CustomMapPlan.innerHTML='<img src="./buttons/plan_orange_75.png">';GEvent.addDomListener(CustomMapPlan,"click",function(){map.setMapType(G_NORMAL_MAP);});varCustomMapSatellite=document.createElement("div");this.setButtonStyle_(CustomMapSatellite);container.appendChild(CustomMapSatellite);CustomMapSatellite.innerHTML='<img src="./buttons/satellite_orange_75.png">';GEvent.addDomListener(CustomMapSatellite,"click",function(){map.setMapType(G_SATELLITE_MAP);});varCustomMapRelief=document.createElement("div");this.setButtonStyle_(CustomMapRelief);container.appendChild(CustomMapRelief);CustomMapRelief.innerHTML='<img src="./buttons/relief_orange_75.png">';GEvent.addDomListener(CustomMapRelief,"click",function(){map.setMapType(G_PHYSICAL_MAP);});map.getContainer().appendChild(container);returncontainer;}CustomMapControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_TOP_RIGHT,newGSize(15,15));}CustomMapControl.prototype.setButtonStyle_=function(button){button.style.marginBottom="8px";button.style.cursor="pointer";}functionLogoControl(){}LogoControl.prototype=newGControl();LogoControl.prototype.initialize=function(map){varcontainer=document.createElement("div");varLogo=document.createElement("div");container.appendChild(Logo);Logo.innerHTML='<img src="./logos/logo_geotribu.png">';map.getContainer().appendChild(container);returncontainer;}LogoControl.prototype.getDefaultPosition=function(){returnnewGControlPosition(G_ANCHOR_BOTTOM_LEFT,newGSize(map.getSize().width/7,0));}map.addControl(newZoomControl());map.addControl(newPanUpControl());map.addControl(newPanDownControl());map.addControl(newPanRightControl());map.addControl(newPanLeftControl());map.addControl(newCustomMapControl());map.addControl(newLogoControl());map.setCenter(newGLatLng(43.57691664771851,1.402451992034912),13);map.enableScrollWheelZoom();map.enableContinuousZoom();varkml=newGGeoXml("http://88.191.39.115/fabien/geotribu/kml/route.kml");map.addOverlay(kml);varmarker1=newGMarker(newGLatLng(43.57769664771851,1.402821992034912));map.addOverlay(marker1);GEvent.addListener(marker1,'click',function(){marker1.showMapBlowup({zoomLevel:18,mapType:G_SATELLITE_MAP});});varmarker2=newGMarker(newGLatLng(43.53507914503071,1.493223511978854));map.addOverlay(marker2);GEvent.addListener(marker2,'click',function(){marker2.showMapBlowup({zoomLevel:18,mapType:G_SATELLITE_MAP});});}else{alert('Désolé, mais votre navigateur n\'est pas compatible avec Google Maps');}}</script></head><bodyonload="initialize()"onunload="GUnload()"><divid="map"></div></body></html>
Le serveur hébergeant la démonstration n'étant plus disponible depuis de nombreuses années, la démonstration, autre fois intégrée en iFrame est désactivée. <iframe src="http://88.191.39.115/fabien/geotribu/%5bgeotribu%5d_Google-Maps_tuto8.html" height="350px" width="100%"></iframe>
Toujours se référer à l'API Google Maps - Google Maps API Reference pour les différentes classes, méthodes et options utilisées. Le fichier XML doit toujours être une URL accessible. Consulter cette page pour les spécifications des fichiers KML.
Ce tutoriel décrit les étapes pour ajouter un fichier KML à la carte et pour afficher une mini-carte dans une infobulle. Les possiblités d'utilisation de superposition d'un fichier sont nombreuses - exemple le projet Inde à vélo.
Informaticien et géographe de formation, je suis j'étais indépendant en base de données, webmapping, j'aide à la modélisation et à la définition du besoin client, je développe j'encadre le développement des interfaces d'administration et des carto-web, je conçois des api géographiques pour les administrations, les ONG, les labos de recherche. Je suis maintenant responsable de l'innovation chez Sogefi. Toujours des cartes et des données qui ont besoin de communiquer. Nous avons une spécialité dans les données foncières et de leur représentation à destination des administrations.
J'aime comprendre les données et en faire quelquechose ; du coup je fais pas mal de veille techno sur tout ce qui touche aux technologies web de dataviz - carto ou non.
J'ai une préférence pour les technologies libres. Tout ce qui touche aux interactions hommes-machines avec des technos comme Kinect, Arduino, Raspberry, les imprimantes 3d ou les machines de découpe laser, les caméras 360° me passionne. Et j'essaie de les utiliser dans le cadre du travail.
Commentaires
Une version minimale de la syntaxe markdown est acceptée pour la mise en forme des commentaires. Propulsé par Isso.