第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何為列表中的每個(gè)標(biāo)記創(chuàng)建單獨(dú)的監(jiān)聽器

如何為列表中的每個(gè)標(biāo)記創(chuàng)建單獨(dú)的監(jiān)聽器

郎朗坤 2023-10-14 18:18:30
一旦你這樣做:const currencies = [euro, yen];沒有從back 到或從back 到 的鏈接。獲取和變量的值并將這些值放入數(shù)組中。currencies[0]eurocurrencies[1]yen[euro, yen]euroyen嘗試對(duì)您所擁有的進(jìn)行最小的更改,您可以使用對(duì)象而不是數(shù)組:for (let [currencyName, currencyValue] of Object.entries({euro, yen})) {        const pair = await fetchPairData(currencyValue, dollar);    const route = new Route([pair], dollar);    console.log(currencyName, currencyValue + route.midPrice.toSignificant(6));}我有一個(gè)坐標(biāo)列表,通過它在地圖上設(shè)置標(biāo)記。標(biāo)記僅以小于 12 的放大倍數(shù)出現(xiàn)在地圖上。我想使當(dāng)我單擊標(biāo)記時(shí)執(zhí)行某些功能,并且僅執(zhí)行一次?,F(xiàn)在事情是這樣的:for (let i = 0; i < locationsCoordinates.length; i++) {    const bridge = locationsCoordinates[i];    markers.push( new google.maps.Marker({        position: bridge[1],        title: bridge[0],        map,        icon: 'img/bridges.png',        clickable: true,    }));}google.maps.event.addListener(map, 'zoom_changed', function() {    let zoom = map.getZoom();    if (zoom < 12) {        for (let i = 0; i < markers.length; i++) {            markers[i].setVisible(false);            markers[i].addListener("click", function (event) {                test +=1;            });            }        }     else {        for (let i = 0; i < markers.length; i++) {            markers[i].setVisible(true);        }    }})問題是,對(duì)于這樣的代碼,當(dāng)您單擊任何標(biāo)記時(shí),該函數(shù)會(huì)運(yùn)行多次。
查看完整描述

2 回答

?
慕桂英3389331

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

您需要使用addListenerOnce并稍微重構(gòu)您的代碼。像這樣的東西

var locationsCoordinates = [{lat: 12.84, lng: 122.89}, {lat: 12.80, lng: 122.93}, {lat: 12.74, lng: 122.85}];


var markers = [];


for (let i = 0; i < locationsCoordinates.length; i++) {

? ? var marker = new google.maps.Marker({

? ? ? ? position: locationsCoordinates[i],

? ? ? ? map

? ? });

? ??

? ? google.maps.event.addListenerOnce(marker, "click", () => {

? ? ? ? console.log("marker", i, "clicked");

? ? });

? ??

? ? markers.push(marker);

}


google.maps.event.addListener(map, 'zoom_changed', function() {

? ? let zoom = map.getZoom();

? ? if (zoom < 12) {

? ? ? ? for (let i = 0; i < markers.length; i++) {

? ? ? ? ? ? markers[i].setVisible(false);

? ? ? ? }

? ? } else {

? ? ? ? for (let i = 0; i < markers.length; i++) {

? ? ? ? ? ? markers[i].setVisible(true);

? ? ? ? }

? ? }

})


查看完整回答
反對(duì) 回復(fù) 2023-10-14
?
慕容森

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超18個(gè)贊

創(chuàng)建標(biāo)記時(shí),將偵聽器添加到標(biāo)記,如相關(guān)示例中所示。不在zoom_changed地圖上的事件監(jiān)聽器中。

不確定為什么要在標(biāo)記隱藏時(shí)添加偵聽器(.setVisible(false)大小寫),而不是在顯示標(biāo)記時(shí)添加偵聽器,但如果在創(chuàng)建標(biāo)記時(shí)將偵聽器添加到標(biāo)記,則不需要執(zhí)行任何特殊操作。

相關(guān)問題使用函數(shù)閉包將標(biāo)記與其InfoWindow內(nèi)容相關(guān)聯(lián)(或者單擊標(biāo)記時(shí)您想要執(zhí)行的任何操作),下面的代碼使用關(guān)鍵字let,這是該問題的現(xiàn)代解決方案,但函數(shù)更接近可以用作好吧,特別是如果您需要支持舊版瀏覽器。

  for (let i = 0; i < locationsCoordinates.length; i++) {

    const bridge = locationsCoordinates[i];

    let marker = new google.maps.Marker({

      position: bridge[1],

      title: bridge[0],

      map,

      icon: 'img/bridges.png',

      clickable: true,

    });

    markers.push(marker);

    google.maps.event.addListener(marker, 'click', function(evt) {

      infowindow.setContent(locationsCoordinates[i][0]);

      infowindow.open(map, marker);

      test += 1;

    });

  }

  google.maps.event.addListener(map, 'zoom_changed', function() {

    let zoom = map.getZoom();

    if (zoom < 12) {

      for (let i = 0; i < markers.length; i++) {

        markers[i].setVisible(false);

      }

    } else {

      for (let i = 0; i < markers.length; i++) {

        markers[i].setVisible(true);

      }

    }

  })

概念證明小提琴

代碼片段:

// The following example creates markers to indicate beaches near

// Sydney, NSW, Australia. 

let markers = [];

let test = 0;


function initMap() {

  const map = new google.maps.Map(document.getElementById("map"), {

    zoom: 10,

    center: {

      lat: -33.9,

      lng: 151.2

    },

  });

  let infowindow = new google.maps.InfoWindow();

  // Adds markers to the map.

  for (let i = 0; i < locationsCoordinates.length; i++) {

    const bridge = locationsCoordinates[i];

    let marker = new google.maps.Marker({

      position: bridge[1],

      title: bridge[0],

      map,

      clickable: true,

    });

    markers.push(marker);

    google.maps.event.addListener(marker, 'click', function(evt) {

      infowindow.setContent(locationsCoordinates[i][0]);

      infowindow.open(map, marker);

      test += 1;

      console.log("open infowindow:" + this.getTitle() + " test=" + test);

    });

  }

  google.maps.event.addListener(map, 'zoom_changed', function() {

    let zoom = map.getZoom();

    if (zoom < 12) {

      infowindow.close();

      for (let i = 0; i < markers.length; i++) {

        markers[i].setVisible(false);

      }

    } else {

      for (let i = 0; i < markers.length; i++) {

        markers[i].setVisible(true);

      }

    }

  })

}

// Data for the markers consisting of a name and a LatLng.

const locationsCoordinates = [

  ["Bondi Beach", {

    lat: -33.890542,

    lng: 151.274856

  }],

  ["Coogee Beach", {

    lat: -33.923036,

    lng: 151.259052

  }],

  ["Cronulla Beach", {

    lat: -34.028249,

    lng: 151.157507

  }],

  ["Manly Beach", {

    lat: -33.80010128657071,

    lng: 151.28747820854187

  }],

  ["Maroubra Beach", {

    lat: -33.950198,

    lng: 151.259302

  }],

];

/* Always set the map height explicitly to define the size of the div

       * element that contains the map. */


#map {

  height: 100%;

}



/* Optional: Makes the sample page fill the window. */


html,

body {

  height: 100%;

  margin: 0;

  padding: 0;

}

<!DOCTYPE html>

<html>


<head>

  <title>Complex Marker Icons</title>

  <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>

  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=&v=weekly" defer></script>

  <!-- jsFiddle will insert css and js -->

</head>


<body>

  <div id="map"></div>

</body>


</html>


查看完整回答
反對(duì) 回復(fù) 2023-10-14
  • 2 回答
  • 0 關(guān)注
  • 182 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)