if(!Twi.isObject("carte")){ Twi.addObject("carte"); Twi.carte = new function() { /********************************************************/ /* Core */ /********************************************************/ var name = "carte"; this.imagecentre = Twi.options.sourceScript + Twi.options.client +"-"+ Twi.options.key +"/carte/images/marker/marker_centre.png"; this.map = {}; this.streetview = {}; this.setIconeCentre = function (url){ this.imagecentre = url; } this.scriptgoogle = function (fct,key){ if(!Twi.apigoogle){ Twi.apigoogle = true; var cssFile = "https://unpkg.com/leaflet@1.3.4/dist/leaflet.css"; var jsFile = "https://unpkg.com/leaflet@1.3.4/dist/leaflet.js"; document.head.innerHTML = document.head.innerHTML + ''; var script = document.createElement("script"); if (script.readyState){ script.onreadystatechange = function(){ if (script.readyState == "loaded" || script.readyState == "complete"){ script.onreadystatechange = null; setTimeout(fct+"()", 500); var scriptFS = document.createElement("script"); scriptFS.src = 'https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/Leaflet.fullscreen.min.js'; document.getElementsByTagName("head")[0].appendChild(scriptFS); } }; }else{ script.onload = function(){ setTimeout(fct+"()", 500); var scriptFS = document.createElement("script"); scriptFS.src = 'https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/Leaflet.fullscreen.min.js'; document.getElementsByTagName("head")[0].appendChild(scriptFS); }; } script.src = jsFile; document.getElementsByTagName("head")[0].appendChild(script); var linkFS = document.createElement("link") linkFS.rel = 'stylesheet'; linkFS.href = 'https://api.mapbox.com/mapbox.js/plugins/leaflet-fullscreen/v1.0.1/leaflet.fullscreen.css'; document.getElementsByTagName("head")[0].appendChild(linkFS); var link = document.createElement("link") link.rel = 'stylesheet'; link.href = cssFile; document.getElementsByTagName("head")[0].appendChild(link); }else{ if (!window['google']) { window['google'] = {}; } if (window['google']['maps']) { setTimeout(fct+"()", 500); }else{ setTimeout('Twi.carte.scriptgoogle("'+fct+'")', 500); } } } this.init = function (key) { if(key === undefined) key = ''; Twi.setWindowsOnload(function(){ Twi.carte.scriptgoogle("Twi.carte.initMap", key); }); } this.zoneTagCacher = function (id_carte,tagcacheid){ if(!this.map[id_carte].option.tagcacheid){ this.map[id_carte].option["tagcacheid"] = tagcacheid; } } this.resizeCarte = function(id){ //google.maps.event.trigger(document.getElementById('tw_carte_'+id), 'resize'); } this.div2parties = function(div){ var divmap = document.createElement('div'); divmap.id = "tw_carte_"+div.id; divmap.className = "tw_carte"; div.appendChild(divmap); attribut = Twi.getAttributes(div); if(attribut["supprimerLegendeTwimmo"]){ if(attribut["supprimerLegendeTwimmo"].value=="Oui"){ return { markers : {} , map : {}, carte : divmap, option : {} }; } } var divlegende = document.createElement('div'); divlegende.id = "tw_legende_"+div.id; divlegende.className = "tw_legende"; div.appendChild(divlegende); return { markers : {} , map : {}, carte : divmap, legende : divlegende, option : {}, bien : {} }; } this.insereDiv = function (id) { if (arguments.length > 1) { for (var i = 0; i < arguments.length; i++){ this.map[arguments[i]] = this.div2parties(this.chercherElement(arguments[i])); } return true; } if(this.isString(id)){ this.map[id] = this.div2parties(this.chercherElement(id)); return true; } return false; } this.initMap = function (){ try { for (var property in this.map){ var myLatlng = [46.22,2.21]; var myOptions = { zoom: 5, center: myLatlng, mapTypeId: 'ROADMAP' } attribut = this.getAttributes(property); if(attribut["lng"]){ lng = attribut["lng"].value;}else{ lng = "";} if(attribut["lat"]){ lat = attribut["lat"].value;}else{ lat = "";} if(attribut["minimumZoom"]){ this.map[property].option["minZoom"]=parseInt(attribut["minimumZoom"].value,10); } if(attribut["maximumZoom"]){ this.map[property].option["maxZoom"]=parseInt(attribut["maximumZoom"].value,10); } if(attribut["echelle"]){ if(attribut["echelle"].value=="Oui"){ myOptions["scaleControl"]= true; } } if(attribut["streetview"]){ if(attribut["streetview"].value=="Oui"){ myOptions["streetViewControl"]= true; }else{ myOptions["streetViewControl"]= false; } }else{ myOptions["streetViewControl"] = true; } if(attribut["zoom"]){ myOptions["zoom"] = parseInt(attribut["zoom"].value,10); }else{ myOptions["zoom"] = 5; } if(attribut["mapTypeId"]){ myOptions["mapTypeId"] = eval(attribut["mapTypeId"].value); } if(attribut["lng"] && attribut["lat"]){ var latlng = [attribut["lat"], attribut["lng"]]; myOptions["center"] = latlng; } if(attribut["supprimerGuiDefaut"]){ if(attribut["supprimerGuiDefaut"].value=="Oui"){ myOptions["disableDefaultUI"]= true; } } if( attribut["adresse"] || attribut["cp"] || attribut["ville"] || attribut["pays"] ){ if(attribut["adresse"]){ adresse=attribut["adresse"].value; adresse=adresse.replace("'", " "); }else{ adresse=""; } if(attribut["cp"]){ cp=attribut["cp"].value; cp=cp.replace("'", " "); }else{ cp=""; } if(attribut["ville"]){ ville=attribut["ville"].value; ville=ville.replace("'", " "); }else{ ville=""; } if(attribut["pays"]){pays=attribut["pays"].value;}else{pays="France";} this.adresseToGPS(property,myOptions,adresse,cp,ville,pays,myOptions["zoom"]); }else{ this.insercarte(property,myOptions); } } }catch(err){ setTimeout("Twi.carte.initMap()", 500); } } this.insercarte = function (id_carte,myOptions){ var cartevisite = true; var cartestyle; if(this.map[id_carte].option["tagcacheid"]){ cartestyle = document.getElementById(this.map[id_carte].option["tagcacheid"]); if(cartestyle.style.display == "none"){ cartestyle.style.display="block"; cartevisite = false; } } //Map Twi.carte.map[id_carte].map = L.map(Twi.carte.map[id_carte].carte,{fullscreenControl: {pseudoFullscreen: false}}).setView([myOptions.center[0],myOptions.center[1]], 10); //new google.maps.Map(Twi.carte.map[id_carte].carte, myOptions); L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 19, attribution: ' OpenStreetMap | Twimmo Pro' }).addTo(Twi.carte.map[id_carte].map); /*var logo = document.createElement('img'); logo.src="https://api.twimmo.com/images/logoPf.png"; logo.style.width="63px"; logo.style.margin="0px"; var lien = document.createElement('a'); lien.href = "https://www.twimmo.com"; lien.title = 'En partenariat avec Twimmo'; lien.appendChild(logo); lien.target = '_blank'; var homeControlDiv = document.createElement('DIV'); homeControlDiv.appendChild(lien); homeControlDiv.index = 1; Twi.carte.map[id_carte].map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(homeControlDiv);*} //Gestion de Event {*google.maps.event.addListener(Twi.carte.map[id_carte].map, 'zoom_changed', function(){ zoomChangeBoundsListener = google.maps.event.addListener(Twi.carte.map[id_carte].map, 'bounds_changed', function(event) { //if (Twi.carte.map[id_carte].map.getZoom() > 16){ // Twi.carte.map[id_carte].map.setZoom(16); //} google.maps.event.removeListener(zoomChangeBoundsListener); }); if(Twi.isNumber(Twi.carte.map[id_carte].option["minZoom"])){ if (Twi.carte.map[id_carte].map.getZoom() < Twi.carte.map[id_carte].option["minZoom"]) { Twi.carte.map[id_carte].map.setZoom(Twi.carte.map[id_carte].option["minZoom"]); } } if(Twi.isNumber(Twi.carte.map[id_carte].option["maxZoom"])){ if (Twi.carte.map[id_carte].map.getZoom() > Twi.carte.map[id_carte].option["maxZoom"]) { Twi.carte.map[id_carte].map.setZoom(Twi.carte.map[id_carte].option["maxZoom"]); } } Twi.carte.mouvementCarte(id_carte); }); Twi.carte.map[id_carte].map google.maps.event.addListener(Twi.carte.map[id_carte].map, 'drag', function() { Twi.carte.mouvementCarte(id_carte); });*/ //google.maps.event.trigger(Twi.carte.map[id_carte].map, 'resize'); Twi.carte.map[id_carte].map.on('zoomend', function() { Twi.carte.mouvementCarte(id_carte); }); Twi.carte.map[id_carte].map.on('moveend', function() { Twi.carte.mouvementCarte(id_carte); }) //Marker Twi.carte.map[id_carte].bien = Twi.carte.setMarkers(id_carte,myOptions["center"],"","",Twi.carte.imagecentre,this.options.sourceScript + this.options.client +"-"+ this.options.key +"/carte/images/marker/marker_shadow.png"); if(!cartevisite){ window.setTimeout(function() { cartestyle.style.display = "none"; }, 500); } } /********************************************************/ /* Legende */ /********************************************************/ this.initcarteLegende = function (){ for (var property in this.map){ if(this.map[property].legende){ var data ={ id_div : this.map[property].legende.id, id_carte : property }; this.execute ("carte","core/legende",data); } } } this.clicLegende = function(valeur,etat){ var reg=new RegExp("[\|]+", "g"); var tableau=valeur.split(reg); if(etat){ var reg=new RegExp("[\|]+", "g"); var tableau=valeur.split(reg); var data ={ id_carte : tableau[0], type : tableau[1], lat : this.map[tableau[0]].map.getCenter().lat, lng : this.map[tableau[0]].map.getCenter().lng }; this.execute ("carte","flux/poi",data); }else{ this.supprimerMarker(tableau[0],tableau[1]); } } /********************************************************/ /* Marker */ /********************************************************/ this.mouvementCarte = function(id_carte){ var carte = this.map[id_carte]; for (var property in carte.markers){ var data ={ id_carte : id_carte, type : property, lat : carte.map.getCenter().lat, lng : carte.map.getCenter().lng }; this.execute ("carte","flux/poi",data); } } this.constuireMarker = function(id_carte,type,data){ var img = this.options.sourceScript + this.options.client +"-"+ this.options.key +"/carte/images/marker/marker_"+type+".png"; var imgombre = this.options.sourceScript + this.options.client +"-"+ this.options.key +"/carte/images/marker/marker_shadow.png"; for (var property in data){ var reg=new RegExp("[-]+", "g"); var tableau=property.split(reg); var type = tableau[0]; var id = tableau[1]; if(!this.map[id_carte].markers[type]){ this.map[id_carte].markers[type]= {}; } if(!this.map[id_carte].markers[type][id]){ var latlng = [data[property].lat,data[property].lng]; this.map[id_carte].markers[type][id] = this.setMarkers(id_carte,latlng,data[property].titre,data[property].html,img,imgombre); } } } this.setMarkers = function (id_carte,latlng,titre,html,img,imgombre) { var options = {}; if(img){ options.icon = L.icon({ iconUrl: img, iconSize: [25,45], iconAnchor: [12,45], shadowUrl: imgombre, shadowSize: [25,45], shadowAnchor: [7,45], }); } var marker = L.marker(latlng, options).addTo(this.map[id_carte].map); if(html){ marker.bindPopup("
" + html + "
"); } return marker; } this.supprimerMarker = function(id_carte,type){ if(this.map[id_carte].markers[type]){ var famillemarker = this.map[id_carte].markers[type]; for (var property in famillemarker){ this.map[id_carte].map.removeLayer(famillemarker[property]) //famillemarker[property].setMap(null); delete famillemarker[property]; } delete this.map[id_carte].markers[type]; } } /********************************************************/ /* Service */ /********************************************************/ this.adresseToGPS = function(id_carte,myOptions,adresse,cp,ville,pays,zoom){ var goadresse = adresse; if(cp!=""){ goadresse = goadresse + ", "+ cp; } if(ville!=""){ goadresse = goadresse + ", "+ ville; } if(pays!=""){ goadresse = goadresse + ", "+ pays; }else{ goadresse = goadresse + ", France"; } Twi.setEspaceCache("geogoogle"); if(Twi.cache.geogoogle[goadresse.toLowerCase()]){ myOptions["center"] = [cache.geogoogle[goadresse.toLowerCase()].lat,Twi.cache.geogoogle[goadresse.toLowerCase()].lng]; myOptions["zoom"] = zoom; Twi.carte.insercarte(id_carte,myOptions); }else{ var geocode = 'https://api-adresse.data.gouv.fr/search/?q=' + goadresse; var request = new XMLHttpRequest(); request.open('GET', geocode); request.responseType = 'json'; request.send(); request.onload = function() { var apiadresse = request.response; if(apiadresse.features && apiadresse.features[0]){ myOptions["center"] = [apiadresse.features[0].geometry['coordinates'][1], apiadresse.features[0].geometry['coordinates'][0]] myOptions["zoom"] = zoom; Twi.cache.geogoogle[goadresse.toLowerCase()] = { lat : myOptions["center"][0], lng : myOptions["center"][1] } Twi.carte.insercarte(id_carte,myOptions); }else{ alert("Echec Geocode"); } }; } } this.zoomMoin = function(id_carte){ var zoom = this.map[id_carte].map.getZoom() - 1; this.setZoom(id_carte,zoom); } this.zoomPlus = function(id_carte){ var zoom = this.map[id_carte].map.getZoom() + 1; this.setZoom(id_carte,zoom); } this.setZoom = function(id_carte,valeur){ this.map[id_carte].map.setZoom(parseInt(valeur,10)); } this.getZoom = function(id_carte){ return this.map[id_carte].map.getZoom(); } this.geocodeAdresse = function(id_carte,adresse,apigoogle){ if(apigoogle){ Twi.setEspaceCache("geogoogle"); if(Twi.cache.geogoogle[adresse.toLowerCase()]){ Twi.carte.map[id_carte].map.setCenter([cache.geogoogle[adresse.toLowerCase()].lat,Twi.cache.geogoogle[adresse.toLowerCase()].lng]); Twi.carte.map[id_carte].map.setZoom(12); }else{ var geocode = 'https://api-adresse.data.gouv.fr/search/?q=' + address; var request = new XMLHttpRequest(); request.open('GET', geocode); request.responseType = 'json'; request.send(); request.onload = function() { var apiadresse = request.response; if(apiadresse.features && apiadresse.features[0]){ Twi.cache.geogoogle[goadresse.toLowerCase()] = { lat : apiadresse.features[0].geometry['coordinates'][1], lng : apiadresse.features[0].geometry['coordinates'][0] } Twi.carte.map[id_carte].map.setCenter([apiadresse.features[0].geometry['coordinates'][1], apiadresse.features[0].geometry['coordinates'][0]]); Twi.carte.map[id_carte].map.setZoom(12); }else{ alert("Echec Geocode"); } }; } }else{ } } this.directionAdresse = function(id_carte,adresseDepart,adresseArriver){ var directionsService = new google.maps.DirectionsService(); var directionsDisplay = new google.maps.DirectionsRenderer(); directionsDisplay.setMap(this.map[id_carte].map); var request = { origin:adresseDepart, destination:adresseArriver, travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); } this.infoAdresseFonction = function(id_carte,adresse,okfonction,pbfonction){ var bounds = new google.maps.LatLngBounds(); var geocoder = new google.maps.Geocoder(); geocoder.geocode({ 'address': adresse}, function(results, status) { if (status == google.maps.GeocoderStatus.OK){ var data ="{"; for (i=0 ; i<= results[0].address_components.length-1 ; i++){ if (results[0].address_components[i].types == 'street_number') data+="numero:\""+ results[0].address_components[i].long_name+"\","; else if (results[0].address_components[i].types == 'route') data+="rue:\""+ results[0].address_components[i].long_name+"\","; else if (results[0].address_components[i].types == 'locality,political') data+="ville:\""+results[0].address_components[i].long_name+"\","; else if (results[0].address_components[i].types == 'postal_code') data+="cp:\""+results[0].address_components[i].long_name+"\","; } data+="lat:"+results[0].geometry.location.lat()+","; data+="lng:"+results[0].geometry.location.lng()+"}"; bounds.extend(results[0].geometry.location); Twi.carte.map[id_carte].map.setCenter(results[0].geometry.location); Twi.carte.map[id_carte].map.fitBounds(bounds); if(Twi.carte.map[id_carte].bien){ Twi.carte.map[id_carte].bien.setMap(null); } Twi.carte.map[id_carte].bien = Twi.carte.setMarkers(id_carte,results[0].geometry.location,"","",Twi.carte.imagecentre,""); eval('okfonction('+data+')'); }else{ eval('pbfonction('+status+')'); } }); return false; } /********************************************************/ /* StreetView */ /********************************************************/ this.streetViewAdresse = function(id,x,y,adresse){ adresse = adresse + ", France"; this.streetview[id] = { id : id, largeur : x, hauteur : y, adresse : adresse, getStreetviewAdresse : function(){ Twi.setEspaceCache("geogoogle"); if(Twi.cache.geogoogle[adresse.toLowerCase()]){ Twi.carte.streetView(id,x,y,Twi.cache.geogoogle[adresse.toLowerCase()].lat,Twi.cache.geogoogle[goadresse].lng); }else{ var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': adresse}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { Twi.cache.geogoogle[adresse.toLowerCase()] = { lat : results[0].geometry.location.lat(), lng : results[0].geometry.location.lng() } Twi.carte.streetView(id,x,y,results[0].geometry.location.lat(),results[0].geometry.location.lng()); /* var panoramaOptions = { position: new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng()), pov: { heading: 34, pitch: 10, zoom: 1 } }; document.getElementById(id).style.width=x; document.getElementById(id).style.height=y; var panorama = new google.maps.StreetViewPanorama(document.getElementById(id),panoramaOptions); panorama.setVisible(true); */ }else{ alert("Geocode n'a pas ??t?? retenue pour les raisons suivantes :" + status); } }); } } } Twi.setWindowsOnload(function(){ Twi.carte.scriptgoogle("Twi.carte.streetview."+id+".getStreetviewAdresse"); }); } this.streetView = function(id,x,y,lat,lng){ if(id && lat && lng){ this.streetview[id] = { id : id, largeur : x, hauteur : y, adresse : "", getStreetview : function(info){ var lecteur = ""; if(info){ if(Twi.getInfoBrowser().browser == "msie"){ lecteur = ''; }else{ lecteur =''; lecteur +='' lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; lecteur +=''; } document.getElementById(this.id).innerHTML = lecteur; } } }; var srcgooglemap = "https://maps.google.com/cbk?output=json&oe=utf-8&ll="+escape(lat)+"%2C"+escape(lng)+"&callback=Twi.carte.streetview."+id+".getStreetview"; Twi.get(srcgooglemap,false); } } /********************************************************/ /* Init */ /********************************************************/ for (var property in Twi){ if(property!=this.name){ this[property] = Twi[property]; } } } }