Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
272 views
in Technique[技术] by (71.8m points)

javascript - How do I generate several different MapBox maps

I'm trying to build a website where a user types in a city and venue and a bunch of venues load up with maps in each venue container. What happens is that only the markers show up and only the last venue on the list shows the actual map, the other map containers are blank.

Picture on what happens.

Code that I tried:

mapboxgl.accessToken = 'ACCESSTOKEN123';
let maps = {}, markers = {};

export function loadVenues(venues, container) {
  container.innerHTML = "";
  venues.forEach(venue => {
    const {
      name,
      id,
      lat,
      lng
    } = venue;

    // ADDING VENUE
    container.innerHTML += `
      <div class="col">
        <div class="venue" id="venue${id}">

          <h3 class="card-title">${name}</h3>

          <div class="map-content">
            <div id=${id} class='venue-map'></div>

            <button class="btn btn-secondary rounded-circle venue-point">
              <img src="./style/images/icons8_next_page_100px.png" alt="">
            </button>
          </div>

        </div>
      </div>
    `;

    // MAKING MAP
    maps = {
      ...maps,
      ["map" + id]: (new mapboxgl.Map({
        container: id,
        center: [lng, lat],
        style: 'mapbox://styles/mapbox/dark-v10',
        zoom: 15
        }))
    };

    // MAKING MARKER
    markers = {
      ...markers,
      ["marker" + id]: (new mapboxgl.Marker({
        color: "#5B43EF",
      })
      .setLngLat([lng, lat])
      .addTo(maps["map" + id]))
    };

  });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do you have a JSFiddle or JSbin where you're testing this? If so, can you please post it here along with your question? It seems as though there may be an issue with your forEach loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...