IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques & Frameworks Discussion :

Manipuler une carte avec JavaScript


Sujet :

Bibliothèques & Frameworks

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    335
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 335
    Par défaut Manipuler une carte avec JavaScript
    bonsoir,

    je voudrais faire la chose suivante en JS :

    1. on est au départ sur une image centrée sur Paris (France);
    2. on clique sur la guyane dans une liste à droite;
    3. on a une image déformée qui s'affiche. l'image est déformée pour montrer que l'on se déplace sur le globe;
    4. on arrive sur l'image définitive centrée sur la guyane; la guyane sera de couleur différente.



    est-ce possible en JS ? si ce n'est pas possible n'importe quel framework irait

    merci de votre aide

  2. #2
    bm
    bm est déconnecté
    Membre extrêmement actif

    Homme Profil pro
    Freelance
    Inscrit en
    Octobre 2002
    Messages
    874
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Activité : Freelance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Octobre 2002
    Messages : 874
    Billets dans le blog
    6

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    335
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 335
    Par défaut
    merci

    mais j'ai une autre idée:
    mon truc ne serait pas possible en utilisant leaflet parce qu'en plus je pouurrais afficher grace à cela le trajet entre la france et la guyane pour montrer que c'est loin.

    ce serait mieux pour moi

  4. #4
    bm
    bm est déconnecté
    Membre extrêmement actif

    Homme Profil pro
    Freelance
    Inscrit en
    Octobre 2002
    Messages
    874
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Drôme (Rhône Alpes)

    Informations professionnelles :
    Activité : Freelance
    Secteur : Tourisme - Loisirs

    Informations forums :
    Inscription : Octobre 2002
    Messages : 874
    Billets dans le blog
    6
    Par défaut
    JS est une couche au dessus de OpenStreetMap et Leaflet un plugin ou API.
    Les animations météo sur les cartes sont codés JS.

    Animer une zone géographique avec des capteurs est un très gros projet.


  5. #5
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    335
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 335
    Par défaut
    bonsoir

    merci pour cette aide j'en ai déduis que c'était tout de meme possible en leaflet
    donc voici les codes que j'ai réalisés

    Code js : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    	function market () {
    	     console.log("mark");
    		switch (lang) { 
    			case 'fr':
    			  article1_1.innerHTML = "<link rel='stylesheet' href='https://unpkg.com/leaflet@1.0.3/dist/leaflet.css'   integrity='sha512-07I2e+7D8p6he1SIM+1twR5TIrhUQn9+I6yjqD53JQjFiMf8EtC93ty0/5vJTZGF8aAocvHYNEDJajGdNx1IsQ=='   crossorigin=''/><div id='mar_g'><div id='mapid'></div></div> <script src='https://unpkg.com/leaflet@1.0.3/dist/leaflet.js'   integrity='sha512-A7vV8IFfih/D732iSSKi20u/ooOfj/AGehOKq0f4vLT1Zr2Y+RX7C+w8A1gaSasGtRUZpF/NZgzSAu4/Gc41Lg=='   crossorigin=''></script>";
    			  aside1_1.innerHTML += '';
    			break;
    			case 'en':
    			  article1_1.innerHTML = '';
    			  aside1_1.innerHTML += '';
    			break;
    		}
    		var mymap = L.map('mapid').setView([48.864716, 2.349014], 0);
    	}


    mon code css :
    Code css : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #mar_g,#mar_l {
         margin: 0 auto;
         width: 22%;
    	  display: inline-block;
    	  vertical-align: middle;
    }
    #mar_l {
     
    }
    #mar_g {
     
    }
    #mapid {
         margin: 0 auto;	
         vertical-align: middle;
         width: 20%;
         height : 20%;
         border:1px solid black;
         border-radius: 10%;    
    }

    et mon code html, je ne vous le donne pas, mais vu que j'ai 'écrit mon html dans mon js, vous l'avez quand meme.

    le cercle que j'ai essayé de définir ne s'affiche pas , à la place j'ai tout simplement un trait à peu près au milieu de la page et horizontal.

    comment faire ?

    merci de votre aide

  6. #6
    Modérateur

    Avatar de NoSmoking
    Homme Profil pro
    Inscrit en
    Janvier 2011
    Messages
    17 211
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Isère (Rhône Alpes)

    Informations forums :
    Inscription : Janvier 2011
    Messages : 17 211
    Par défaut
    Bonjour,
    si je m'en réfère à ton post #1, ce que tu cherches à faire est du morphing c'est possible mais très particulier dans ton cas et ils existent des plugins pour cela.

    Maintenant si tu souhaites utiliser Leaflet je doute que tu puisse obtenir un résultat aussi fin mais tu peux toujours jouer sur avec un effet de dézoom/zoom avec un déplacement de la carte suivant le trajet.

    @bm : rien compris de tes interventions

  7. #7
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    335
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 335
    Par défaut Manipuler une carte leaflet avec JavaScript
    Bonjour,

    j'essaye sur ma page de localiser une carte leaflet suivant la destination voulue .
    Les destinations sont déjà entrées et j'ai trouvé suite à quelques consoles.log la valeur de la variable "where" selon chaque cas. Cela dit, maintenant je n'arrive plus à modifier le zoom de la carte en fonction des lieux.


    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    <!DOCTYPE html>
    <html lang="fr">
    <head>
    <meta charset="utf-8">
    <title>[Leaflet] Animation déplacement</title>
    <meta name="author" content="yann" >
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="desciption" content="[Leaflet] Déplacement animé de la carte d'un point à un autre.">
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v1.0.3/leaflet.css">
    <script src="http://cdn.leafletjs.com/leaflet/v1.0.3/leaflet.js"></script>
    <style>
    html, body {
      margin: 0;
      padding: 0;
      font: 1em/1.5 Verdana, sans-serif;
    }
    h1, h2, h3 {
      margin-bottom: .5em;
      color: #069;}
    #main {
      margin: 0 auto;
      max-width: 60em;
      border-top: 1px solid transparent;
    }
    .section {
      margin: 0 2em 1em;
    }
    #cde {
      position: relative;
      margin: 1em auto;
    }
    #cadre_carte {
      position: relative;
      height: 37em;
      margin: 0 auto;
      max-width: 50em;
      border: 5px solid #FFF;
      font-size: 1em;
      box-shadow: 0 2px 4px 2px #CCC;
    }
    #carte {
      height: 20%;
      width: 20%;
    }
    #menu_destination {
      display: inline-block;
      z-index: 100000;
      position: absolute;
      top: 1em;
      right: 1em;
      padding: .125em;
      min-width: 10em;
      border: 1px solid #abc;
      font: .8em/1.5 Verdana,sans-serif;
      background: #fff;
      box-shadow: 2px 2px 5px #ccc;
      cursor: pointer;
      -moz-user-select: none;
    }
    #menu_destination h1 {
      margin: 0;
      padding: 0;
      font-size: 1em;
      font-weight: normal;
      color: #000;
      text-align:center;
    }
    #menu_destination h1:hover {
      background: #f4f4f4;
    }
    #menu_destination ul {
      margin: 0;
      padding: 0;
      list-style: none;
      display:none;
    }
    #menu_destination ul li {
      padding: .25em 1em;
      line-height: 1.5em;
      background: #fff;
    }
    /* separator */
    #menu_destination ul li:empty {
      margin: .125em 0;
      padding: 0 1em 1px;
      background: #abc;
    }
    #menu_destination ul li[data-dest]:hover {
      background: #cde;
    }
    #menu_destination ul hr {
      margin: 0;
      padding: 0;
    }
     
    /* déclaration pour les markers */
    .circle {
      box-sizing: border-box;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: rgba(255,255,255,0.3);
      box-shadow: 0 0 10px #888;
    }
    .circle-marker {
      box-sizing: border-box;
      width: 30px;
      height: 30px;
    }
    .circle-red {
      border: 5px solid #F00;
    }
    .circle-green {
      border: 5px solid #0A0;
    }
    .circle-animate {
      animation-name: show-hide;
      animation-duration: 1s;
      animation-iteration-count: infinite;
      transform: scale(1);
    }
    @keyframes show-hide {
      0%   { transform: scale(1);}
      50%  { transform: scale(.5);}
      100% { transform: scale(1);}
    }
    </style>
    </head>
    <body>
    <div id="main">
      <div class="header">
        <h1>[Leaflet] Animation déplacement</h1>
      </div>
      <div class="section">  
        <p>Déplacement animé de la carte d'un point à un autre.</p>
      </div>
      <div class="section">
        <div id="cadre_carte">
          <div id="carte"></div>
          <div id="menu_destination">
            <h1>Destination</h1>
            <ul>
              <li data-dest="paris"    >Paris</li>
              <li data-dest="cameroun" >Cameroun</li>
              <li data-dest="comores"  >Comores</li>
              <li data-dest="ethiopie" >Ethiopie</li>
              <li data-dest="guadeloupe"  >Guadeloupe</li>
              <li data-dest="guyane" >Guyane</li>
              <li data-dest="reunion"      >La R&eacute;union</li>
              <li data-dest="madagascar"      >Madagascar</li>         
              <li data-dest="martinique"      >Martinique</li>       
              <li data-dest="maurice"      >Ile Maurice</li>     
              <li data-dest="mayotte"      >Mayotte</li>         
              <li data-dest="tahiti"      >Tahiti</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <script>
    var oMap;
    var animEnCours = false;
    var destination = {
     
      'paris':    L.latLng(48.85342, 2.3488),
      'cameroun':      L.latLng(5.76463, 11.75537),
      'comores' :      L.latLng(-12.14, 44.25),
      'ethiopie' :  L.latLng(8.9800, 38.7989),
      'guadeloupe' :      L.latLng(16.265, -61.551),
      'guyane' :      L.latLng(4.144817,-52.8442),
      'reunion' :      L.latLng(-21.1151, 55.5363),
      'madagascar' :      L.latLng(-19.1576, 47.2082),
      'martinique' :      L.latLng(14.64, -61.01669),
      'maurice' :      L.latLng(-20.3484,57.5833),
      'mayotte' :      L.latLng(-12.8096, 45.1307),
      'tahiti' :      L.latLng(-17.6509, -149.426)
    };
    // création des divIcon
    var oIconRed = L.divIcon({
      iconAnchor : [15, 15],
      className: 'circle-marker',
      html: '<div class="circle-animate circle circle-red"></div>'
    });
    var oIconGreen = L.divIcon({
      iconAnchor : [15, 15],
      className: 'circle-marker',
      html: '<div class="circle-animate circle circle-green"></div>'
    });
    // création des markers
    var oMarkerRed   = L.marker( [0,0], {icon: oIconRed});
    var oMarkerGreen = L.marker( [0,0], {icon: oIconGreen});
    // création de la carte
    var zoomInitial = 9; // ce que l'on modifie versus test5 9 au leu de 7
    var zoomTravel  = 3; 
    oMap = L.map('carte', {
        zoomAnimationThreshold: 20, 
         zoomControl: false,          // voir http://leafletjs.com/reference-1.1.0.html#map-zoomcontrol
        duration: 5,
        center: [ 46.8, 1.7 ],
        zoom: 5 
    });
    oMap.addLayer( L.tileLayer(
        'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
        {
          attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }
      ));
    // fonction sur click oMap
    oMap.on('click', function (e) {
      // affiche marker vert au click
      oMarkerGreen.setLatLng([e.latlng.lat, e.latlng.lng]).addTo(this);
      oMarkerGreen.on('click', function (e) {
        this.remove(oMap);
      })
    });
     
    function swapMarker(where){
      // supprime marker rouge
      oMarkerRed.remove(oMap);
      // affiche marker vert
      oMarkerGreen.setLatLng(where).addTo(oMap);
      // autorise autre animation
      animEnCours = false;
    }
     
    function gotoDestination(where) {
      // si animation en cours on quitte
      if (animEnCours) {
        return;
      }
      animEnCours = true;
      var zoomCarte = oMap.getZoom();
      // récupération point de départ
      var pointDepart = oMap.hasLayer(oMarkerGreen) ? oMarkerGreen.getLatLng() : oMap.getCenter();
      // place les marker rouge et vert sur destination et départ
      oMarkerRed.setLatLng(where).addTo(oMap);
      oMarkerGreen.setLatLng(pointDepart).addTo(oMap);
      // calcul distance déplacement
      var distance = oMap.distance(pointDepart, where)/1000;
      // calcul durée déplacement
      var delay = distance / 2000;
      // accés direct si distance < 1000 km
      if (distance < 1000) {
      
        // réajuste position et zoom
       console.log(where); 
       switch(where) {
         default :  oMap.setView(L.latLng(48.85342, 2.3488), zoomInitial);
         break;
         case L.latLng(48.85342, 2.3488):
         oMap.setView(where,15);
         break;
         case L.latLng(16.265, -61.551):
         oMap.setView(where,8);
         break;}
        // change markers
        swapMarker(where);
      } 
      else {
         // défini action en fin de zoom
        oMap.once('zoomend', function () {
          // déplacement vers destination
          setTimeout(function () {
            // défini action en fin de déplacement
            oMap.once('moveend', function () {
              // on zooom sur destination
              setTimeout(function () {
          // réajuste position et zoom
         switch(where) {
         default :  oMap.setView(where, zoomInitial);
         break;
         case 'paris':
         oMap.setView(where,3);
         break;
         case 'guadeloupe':
         oMap.setView(where,9);
         break;
      }
            // change markers
                swapMarker(where);
              }, 500);  
            });
            // déplacement
            oMap.panTo(where, {
              animate: true,
              duration: delay
            });
          }, 500);
        });
        // ajuste le zoom minimum
        if (zoomCarte === zoomTravel) {
          oMap.fireEvent('zoomend');
        } 
        else {
          oMap.setZoom(zoomTravel);
        }
      }
    }
     
    // gestion du menu destinations
    var oTravel = document.querySelector('#menu_destination');
    oTravel.onclick = function (e) {
      var oSrc = e.target;
      var oDest = oSrc.getAttribute("data-dest");
      if (destination[oDest]) {
        this.querySelector('ul').style.display = 'none';
        gotoDestination(destination[oDest]);
      }
      else {
        this.querySelector('ul').style.display = 'block';
      }
    };
    oTravel.onmouseleave = function (e) {
      this.querySelector('ul').style.display = 'none';
    };
    </script>
    </body>
    </html>

    Comme certains points de la carte ne s'affichent pas avec mon zoom initial, j'ai prévu de créer plusieurs variables (zoominitial 2, 3 ...) en fonction de la destination.

    je me demandais alors comment tester la variable "where" pour modifier le zoom suivant la destination .

    j'ai effectué, plusieurs tests mais aucun n'a fonctionné :

    de plus j'ai effectué aujourd'hui tous les tests un par un pour avoir la bonne valeur de zoominitial lorsque l'on clique la 1ère fois :
    paris 3.5; cameroun 3.5; comores 4; ethipoie 30. ; guadeloupe 8; guyane 3.5 ; la reunion 8; madagascar 3.5 ; martinique 8 ; maurice 8 ; mayotte 8 ; tahiti 9 .

    Merci de votre aide

  8. #8
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 680
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 680
    Par défaut
    une 1re modification qui pourrait servir serait de passer l'identifiant du lieu dans la fonction "gotoDestination"
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    gotoDestination(destination[oDest], oDest);
    au alors vous pouvez changer la structure du tableau "destination" pour y stocker le zoom :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    var destination = {
        "paris" : {
            "libelle" : "Paris",
            "point" : L.latLng(48.85342, 2.3488),
            "zoom" : 3.5,
        },
        "cameroun" : {
            "libelle" : "Cameroun",
            "point" : L.latLng(48.85342, 2.3488),
            "zoom" : 3.5,
        },
        "reunion" : {
            "libelle" : "La Réunion",
            "point" : L.latLng(-21.1151, 55.5363),
            "zoom" : 8,
        },
        //...
    };
    j'ai aussi rajouté le libellé dans le tableau ce qui pourrait vous permettre de générer le menu déroulant dynamiquement.

  9. #9
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    335
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 335
    Par défaut
    Bonjour Mathieu,

    J'ai suivi ta proposition de réecrire la variable "destination" :

    Code html : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    <!DOCTYPE html>
    <html lang="fr">
    <head>
    <meta charset="utf-8">
    <title>[Leaflet] Animation déplacement</title>
    <meta name="author" content="yann" >
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="desciption" content="[Leaflet] Déplacement animé de la carte d'un point à un autre.">
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet/v1.0.3/leaflet.css">
    <script src="http://cdn.leafletjs.com/leaflet/v1.0.3/leaflet.js"></script>
    <style>
    html, body {
      margin: 0;
      padding: 0;
      font: 1em/1.5 Verdana, sans-serif;
    }
    h1, h2, h3 {
      margin-bottom: .5em;
      color: #069;}
    #main {
      margin: 0 auto;
      max-width: 60em;
      border-top: 1px solid transparent;
    }
    .section {
      margin: 0 2em 1em;
    }
    #cde {
      position: relative;
      margin: 1em auto;
    }
    #cadre_carte {
      position: relative;
      height: 37em;
      margin: 0 auto;
      max-width: 50em;
      border: 5px solid #FFF;
      font-size: 1em;
      box-shadow: 0 2px 4px 2px #CCC;
    }
    #carte {
      height: 20%;
      width: 20%;
    }
    #menu_destination {
      display: inline-block;
      z-index: 100000;
      position: absolute;
      top: 1em;
      right: 1em;
      padding: .125em;
      min-width: 10em;
      border: 1px solid #abc;
      font: .8em/1.5 Verdana,sans-serif;
      background: #fff;
      box-shadow: 2px 2px 5px #ccc;
      cursor: pointer;
      -moz-user-select: none;
    }
    #menu_destination h1 {
      margin: 0;
      padding: 0;
      font-size: 1em;
      font-weight: normal;
      color: #000;
      text-align:center;
    }
    #menu_destination h1:hover {
      background: #f4f4f4;
    }
    #menu_destination ul {
      margin: 0;
      padding: 0;
      list-style: none;
      display:none;
    }
    #menu_destination ul li {
      padding: .25em 1em;
      line-height: 1.5em;
      background: #fff;
    }
    /* separator */
    #menu_destination ul li:empty {
      margin: .125em 0;
      padding: 0 1em 1px;
      background: #abc;
    }
    #menu_destination ul li[data-dest]:hover {
      background: #cde;
    }
    #menu_destination ul hr {
      margin: 0;
      padding: 0;
    }
     
    /* déclaration pour les markers */
    .circle {
      box-sizing: border-box;
      width: 30px;
      height: 30px;
      border-radius: 50%;
      background-color: rgba(255,255,255,0.3);
      box-shadow: 0 0 10px #888;
    }
    .circle-marker {
      box-sizing: border-box;
      width: 30px;
      height: 30px;
    }
    .circle-red {
      border: 5px solid #F00;
    }
    .circle-green {
      border: 5px solid #0A0;
    }
    .circle-animate {
      animation-name: show-hide;
      animation-duration: 1s;
      animation-iteration-count: infinite;
      transform: scale(1);
    }
    @keyframes show-hide {
      0%   { transform: scale(1);}
      50%  { transform: scale(.5);}
      100% { transform: scale(1);}
    }
    </style>
    </head>
    <body>
    <div id="main">
      <div class="header">
        <h1>[Leaflet] Animation déplacement</h1>
      </div>
      <div class="section">  
        <p>Déplacement animé de la carte d'un point à un autre.</p>
      </div>
      <div class="section">
        <div id="cadre_carte">
          <div id="carte"></div>
          <div id="menu_destination">
            <h1>Destination</h1>
            <ul>
              <li data-dest="paris"    >Paris</li>
              <li data-dest="cameroun" >Cameroun</li>
              <li data-dest="comores"  >Comores</li>
              <li data-dest="ethiopie" >Ethiopie</li>
              <li data-dest="guadeloupe"  >Guadeloupe</li>
              <li data-dest="guyane" >Guyane</li>
              <li data-dest="reunion"      >La R&eacute;union</li>
              <li data-dest="madagascar"      >Madagascar</li>         
              <li data-dest="martinique"      >Martinique</li>       
              <li data-dest="maurice"      >Ile Maurice</li>     
              <li data-dest="mayotte"      >Mayotte</li>         
              <li data-dest="tahiti"      >Tahiti</li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <script>
    var oMap;
    var animEnCours = false;
    var destination = {
        "paris" : {
            "libelle" : "Paris",
            "point" : L.latLng(48.85342, 2.3488),
            "zoom" : 3.5,
        },
        "cameroun" : {
            "libelle" : "Cameroun",
            "point" : L.latLng(48.85342, 2.3488),
            "zoom" : 3.5,
        },
        "comores" : {
            "libelle" : "Comores",
            "point" : L.latLng(-12.14, 44.25),
            "zoom" : 3.5,
        },
        "ethiopie" : {
            "libelle" : "Ethiopie",
            "point" : L.latLng(8.9800, 38.7989),
            "zoom" : 3.5,
        },
        "guadeloupe" : {
            "libelle" : "Guadeloupe",
            "point" : L.latLng(6.265, -61.551),
            "zoom" : 3.5,
        },
        "guyane" : {
            "libelle" : "Guyane",
            "point" : L.latLng(4.144817,-52.8442),
            "zoom" : 3.5,
        },
        "reunion" : {
            "libelle" : "La Réunion",
            "point" : L.latLng(-21.1151, 55.5363),
            "zoom" : 8,
        },
        "madagascar" : {
            "libelle" : "Madagascar",
            "point" : L.latLng(-19.1576, 47.2082),
            "zoom" : 3.5,
        },
        "martinique" : {
            "libelle" : "Martinique",
            "point" : L.latLng(14.64, -61.01669),
            "zoom" : 3.5,
        },
        "maurice" : {
            "libelle" : "Maurice",
            "point" : L.latLng(14.64, -61.01669),
            "zoom" : 3.5,
        },
        "mayotte" : {
            "libelle" : "Mayotte",
            "point" : L.latLng(-12.8096, 45.1307),
            "zoom" : 3.5,
        },
        "tahiti" : {
            "libelle" : "Tahiti",
            "point" : L.latLng(-17.6509, -149.426),
            "zoom" : 3.5,
        },
        //...
    };
    /*var destination = {
     
      'paris':    L.latLng(48.85342, 2.3488),
      'cameroun':      L.latLng(5.76463, 11.75537),
      'comores' :      L.latLng(-12.14, 44.25),
      'ethiopie' :  L.latLng(8.9800, 38.7989),
      'guadeloupe' :      L.latLng(16.265, -61.551),
      'guyane' :      L.latLng(4.144817,-52.8442),
      'reunion' :      L.latLng(-21.1151, 55.5363),
      'madagascar' :      L.latLng(-19.1576, 47.2082),
      'martinique' :      L.latLng(14.64, -61.01669),
      'maurice' :      L.latLng(-20.3484,57.5833),
      'mayotte' :      L.latLng(-12.8096, 45.1307),
      'tahiti' :      L.latLng(-17.6509, -149.426)
    };
    */
    // création des divIcon
    var oIconRed = L.divIcon({
      iconAnchor : [15, 15],
      className: 'circle-marker',
      html: '<div class="circle-animate circle circle-red"></div>'
    });
    var oIconGreen = L.divIcon({
      iconAnchor : [15, 15],
      className: 'circle-marker',
      html: '<div class="circle-animate circle circle-green"></div>'
    });
    // création des markers
    var oMarkerRed   = L.marker( [0,0], {icon: oIconRed});
    var oMarkerGreen = L.marker( [0,0], {icon: oIconGreen});
    // création de la carte
    var zoomInitial = 9; // ce que l'on modifie versus test5 9 au leu de 7
    var zoomTravel  = 3; 
    oMap = L.map('carte', {
        zoomAnimationThreshold: 20, 
         zoomControl: false,          // voir http://leafletjs.com/reference-1.1.0.html#map-zoomcontrol
        duration: 5,
        center: [ 46.8, 1.7 ],
        zoom: 5 
    });
    oMap.addLayer( L.tileLayer(
        'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
        {
          attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
        }
      ));
    // fonction sur click oMap
    oMap.on('click', function (e) {
      // affiche marker vert au click
      oMarkerGreen.setLatLng([e.latlng.lat, e.latlng.lng]).addTo(this);
      oMarkerGreen.on('click', function (e) {
        this.remove(oMap);
      })
    });
     
    function swapMarker(where){
      // supprime marker rouge
      oMarkerRed.remove(oMap);
      // affiche marker vert
      oMarkerGreen.setLatLng(where).addTo(oMap);
      // autorise autre animation
      animEnCours = false;
    }
     
    function gotoDestination(where) {
      // si animation en cours on quitte
      if (animEnCours) {
        return;
      }
      animEnCours = true;
      var zoomCarte = oMap.getZoom();
      // récupération point de départ
      var pointDepart = oMap.hasLayer(oMarkerGreen) ? oMarkerGreen.getLatLng() : oMap.getCenter();
      // place les marker rouge et vert sur destination et départ
      oMarkerRed.setLatLng(where).addTo(oMap);
      oMarkerGreen.setLatLng(pointDepart).addTo(oMap);
      // calcul distance déplacement
      var distance = oMap.distance(pointDepart, where)/1000;
      // calcul durée déplacement
      var delay = distance / 2000;
      // accés direct si distance < 1000 km
      if (distance < 1000) {
      
        // réajuste position et zoom
       console.log(where); 
       switch(where) {
         default :  oMap.setView(L.latLng(48.85342, 2.3488), zoomInitial);
         break;
         case L.latLng(48.85342, 2.3488):
         oMap.setView(where,zoom);
         break;
         case L.latLng(16.265, -61.551):
         oMap.setView(where,8);
         break;}
        // change markers
        swapMarker(where);
      } 
      else {
         // défini action en fin de zoom
        oMap.once('zoomend', function () {
          // déplacement vers destination
          setTimeout(function () {
            // défini action en fin de déplacement
            oMap.once('moveend', function () {
              // on zooom sur destination
              setTimeout(function () {
          // réajuste position et zoom
         switch(where) {
         default :  oMap.setView(where, zoomInitial);
         break;
         case 'paris':
         oMap.setView(where,zoom);
         break;
         case 'guadeloupe':
         oMap.setView(where,9);
         break;
      }
            // change markers
                swapMarker(where);
              }, 500);  
            });
            // déplacement
            oMap.panTo(where, {
              animate: true,
              duration: delay
            });
          }, 500);
        });
        // ajuste le zoom minimum
        if (zoomCarte === zoomTravel) {
          oMap.fireEvent('zoomend');
        } 
        else {
          oMap.setZoom(zoomTravel);
        }
      }
    }
     
    // gestion du menu destinations
    var oTravel = document.querySelector('#menu_destination');
    oTravel.onclick = function (e) {
      var oSrc = e.target;
      var oDest = oSrc.getAttribute("data-dest");
      if (destination[oDest]) {
        this.querySelector('ul').style.display = 'none';
        gotoDestination(destination[oDest]);
      }
      else {
        this.querySelector('ul').style.display = 'block';
      }
    };
    oTravel.onmouseleave = function (e) {
      this.querySelector('ul').style.display = 'none';
    };
    </script>
    </body>
    </html>

    mais je voudrais maintenant savoir où est-ce que cette variable -destination- est utilisée parce que lorsqu'on sélectionne une destination, le point sur la carte ne change pas, ce qui me parait normal vu qu'on a modifié la structure du tableau destination.

    merci de votre aide

  10. #10
    Expert confirmé
    Avatar de mathieu
    Profil pro
    Inscrit en
    Juin 2003
    Messages
    10 680
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Juin 2003
    Messages : 10 680
    Par défaut
    le tableau est utilisé à la ligne 371 pour l'appel à "gotoDestination".
    ensuite dans cette fonction vous aurez le point dans "where["point"]" et la valeur du zoom dans "where["zoom"]"

  11. #11
    Membre éclairé
    Profil pro
    Inscrit en
    Décembre 2012
    Messages
    335
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2012
    Messages : 335
    Par défaut
    Bonsoir Mathieu,

    j'ai tenté d'écrire dans la fonction "gotodestination" dans le script un "console.log" pour afficher le contenu de where.point et where.zoom .

    L'erreur suivante s'affiche lorsque je change de destination :

    TypeError: t is null leaflet.js:5:17888
    que dois faire ?

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 3
    Dernier message: 20/09/2006, 16h07
  2. création d'une carte avec navigation du type mappy
    Par BernardT dans le forum Interfaces Graphiques en Java
    Réponses: 3
    Dernier message: 30/10/2005, 01h24
  3. Inclure une page dans dans une tableau avec javascript
    Par pierrot10 dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 26/09/2005, 12h31
  4. navigation dans une jsp avec javascript
    Par petitelulu dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 15/11/2004, 18h55
  5. Réponses: 4
    Dernier message: 27/04/2004, 14h45

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo