在Mapbox中单击标记时禁用滚动功能

时间:2018-11-17 06:50:45

标签: javascript html scroll mapbox

我一直在遵循Mapbox上可用的教程,以便产生以下代码。我似乎遇到了一个问题,即单击我风格的标记时,地图的缩放功能被禁用。在这里和其他地方阅读了一些问题的答案之后,似乎似乎与getCanvas函数有关,以及它是否从地图或图层中调用。

我不知道如何更改它,因为我对javascript很陌生,因此将非常感谢您的帮助。

我希望我的地图具有与该地图相同的滚动功能:https://www.mapbox.com/bites/00281/

<!DOCTYPE html>
<html>
<head>
  <meta charset='utf-8' />
  <title></title>
  <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
  <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.0/mapbox-gl.js'></script>
  <link href="https://fonts.googleapis.com/css?family=Amatic+SC" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Josefin+Sans" rel="stylesheet">
  <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.49.0/mapbox-gl.css' rel='stylesheet' />
  <style>
    body { margin:0; padding:0; }
    #map { position:absolute; top:0; bottom:0; width:100%}
    
    .mapboxgl-popup-content {
    max-width: 300px;
    padding: 1em;
    }

    h3 {
        color: black;
        font-family: 'Amatic SC', cursive;
        font-weight: bold;
        font-size: 25px;
        text-align: center;
    }

    h4 {
        color: black;
        font-family: 'Amatic SC', cursive;
        font-size: 18px;
        text-align: center;
    }

    h5 {
        color: black;
        font-family: 'Josefin Sans', sans-serif;
        font-weight: lighter;
        font-size: 12px;
        text-align: center;
    }

    p {
        color: black;
        font-family: 'Josefin Sans', sans-serif;
        font-weight: lighter;
        font-size: 12px;
        text-align: center;
    }
</style>
</head>

<body>
<div id='map'></div>
<script>

mapboxgl.accessToken = '';
const map = new mapboxgl.Map({
  container: 'map',
  style: '',
  center: [54.450013, 24.476566],
  zoom: 10.81
});

map.on('load', function() {
    var popup = new mapboxgl.Popup({
        offset: {bottom: [0,0], top: [0,0]},
        closeButton: false,
        closeOnClick: false,
    });

    function showPopup (e) {
        map.getCanvas().style.cursor = 'pointer';

    popup.setLngLat(e.features[0].geometry.coordinates)
    .setHTML(checkEmpty('<center><a href="'+e.features[0].properties.image+'"><img src="'+e.features[0].properties.direct+'" width = "100px"/></a>'+
    '</center><h3>'+e.features[0].properties.restaurant_title+ 
    '</h3><h4>'+e.features[0].properties.macro_location+'</h4>'))
    .addTo(map);
    }

    function hidePopup() {
        map.getCanvas().style.cursor = '';
        popup.remove();
    }

    function checkEmpty(info) {
        return (info) ? info: "No data";
    }

map.on('mouseenter', 'bestbitesad-brunches', showPopup);
map.on('mouseleave', 'bestbitesad-brunches', hidePopup);
});

map.on('click', function(e) {
  var features = map.queryRenderedFeatures(e.point, {
      layers: ['bestbitesad-brunches']
    });

  if (!features.length) {
    return;
  }

  var feature = features[0];

  var popup1 = new mapboxgl.Popup({ 
      offset: [0,0,0,0],
      closeOnClick: true,
      anchor: 'middle'})
    .setLngLat(feature.geometry.coordinates)
    .setHTML( '<center><a href="'+feature.properties.image+'"><img src="'+feature.properties.direct+'" width = "100px"/></a>'+
    '</center><h3>' + feature.properties.restaurant_title + 
    '</h3><h4>' +feature.properties.macro_location+ 
    '</h4><h5>' + feature.properties.contact+
    '</h5><h5>' + feature.properties.times +
    '</h5><p>' + feature.properties.description + 
    '</p><hr><p>' + feature.properties.pricing + '</hr></p>')
    .setLngLat(feature.geometry.coordinates)
    .addTo(map);
});
</script>

</body>
</html>

0 个答案:

没有答案