如何使 Mapbox GL JS 弹出窗口作为侧面板打开

How to make a Mapbox GL JS popup open as a side panel

我正在尝试将 Mapbox GL JS 弹出窗口作为侧面板打开,而不是在相对于标记的位置打开。

我在做某事:

.label {
  color: black;
  font-weight: bold;
}

.popup-header {
  background: blue;
  color: white;
  height: 80px;
  width: 370px;
  margin: -10px;
  border-radius: 5px 5px 0px 0px;
  position: fixed;
  z-index: 900;
}

.popup-header h2 {
  padding: 10px;
}

.address{
  font-weight: bold;
  color: black;
}

p {
  color: gray;
}

h3 {

}

h3:before {
  content: "\f105";
  font-family: "Font Awesome 5 Free";
  color: blue;
  padding-right: 3px;
}

.hidden {
  visibility: hidden;
}

/* Pop-up style */
.mapboxgl-popup-close-button {
  display: block;
  color: white;
  z-index: 1000;
}

.mapboxgl-popup-content {
  font: 400 15px/22px 'Roboto', 'Helvetica Neue', Sans-serif;
  padding: 0;
  width: 350px;
  overflow-y: auto;
  height: 400px;
}

.popup-content-position {
  position: relative;
  top: 70px;
}

.mapboxgl-popup-content-wrapper {
  padding: 1%;
}

.mapboxgl-popup-tip {
 display: none;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset='utf-8' />
    <title>Carte du ski hors piste dans l'Est de l'Amérique du Nord</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
 <!-- JavaScript -->   
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.js'></script>
 <!-- CSS -->   
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css' rel='stylesheet' />
<!--Icons-->   
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.1/css/all.css" integrity="sha384-gfdkjb5BdAXd+lj+gudLWI+BXq4IuLW5IT+brZEZsLFm++aCMlF1V92rMkPaX4PP" crossorigin="anonymous">
    <style>
      body {
        margin: 0;
        padding: 0;
      }

      #map {
        position: absolute;
        top: 0;
        bottom: 0;
        width: 100%;
        z-index: 1000;
      }
    </style>
  </head>
  <body>
    <div id='map'></div>
    <script>
    mapboxgl.accessToken = 'pk.eyJ1IjoibG91aXNjaGF1c2UiLCJhIjoiY2pud2hnaTViMDM2ZzNwbjR1M2UydWIzZCJ9.wELVs_IfGRvQTGvHsdTheA'; // replace this with your access token
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'mapbox://styles/louischause/cjoofi76c0wzh2rmtwsmuovx3', // replace this with your style URL
      center: [-68.560022, 45.972663],
      zoom: 5.59
    });
map.on('click', function(e) {
  var features = map.queryRenderedFeatures(e.point, {
    layers: ['mapow-location'] // replace this with the name of the layer
  });

  if (!features.length) {
    return;
  }

  var feature = features[0];

  var popup = new mapboxgl.Popup({ offset: [0,0] })
    .setLngLat(feature.geometry.coordinates)
    .setHTML('<div class="popup-header"><h2>' + feature.properties.name + '</h2></div><div class="popup-content-position"><p class="address">' + feature.properties.address + '</p><h3>Météo actuelle</h3><p>' + feature.properties.test + '</p><h3>Prévisions météo</h3><p>' + feature.properties.test + '</p><h3>Détails techniques</h3><p><span class="label">Dénivelé</span><br>' + feature.properties.denivele + '</p><p><span class="label">Classification</span><br>' + feature.properties.classification + '</p><p><span class="label">Approche</span><br>' + feature.properties.approche + '</p><p><span class="label">Type de station</span><br>' + feature.properties.type_station + '</p><p><span class="label">Types de remontée offertes</span><br>' + feature.properties.type_remontees + '</p><h3>Détails concernant le prix</h3><p>' + feature.properties.price + '</p><p>' + feature.properties.price_comment + '</p><h3>Informations utiles</h3><p><span class="label">Site web</span><br>' + feature.properties.website + '</p><p><span class="label">Courriel</span><br>' + feature.properties.mail + '</p><p><span class="label">Téléphone</span><br>' + feature.properties.phone + '</p><h3>Photos et vidéos récentes</h3><p>' + feature.properties.instagram_media_embed + '</p></div>')
    .setLngLat(feature.geometry.coordinates)
    .addTo(map);
});
    </script>
  </body>
</html>

如果您单击红色标记,您会看到弹出窗口正好在标记上打开。

我如何编辑我的 js 代码以使其作为侧面板打开(就像我们单击 google 地图上的标记时一样)

这里是我开发的UI我想触发而不是弹窗。 codepen.io/louischausse/pen/oVwgYN

简单的答案是:不要使用弹出窗口。只需编写您自己的代码来响应点击处理程序,并根据需要显示信息。使用 jQuery 可能看起来像:

$('#sidepanel .name').text(feature.properties.name);
$('#sidepanel .address').text(feature.properties.address);

等等。