繁体   English   中英

向标记数组添加声音-Google Map JavaScript

[英]adding sound to an array of markers - google map javascript

我是新来的,所以我知道自己没有信誉。 我是一名艺术家,并且是编程的新手,所以我知道是否没人会接受这一点。 我将其发布在一个偶然的问题上,这是一个简单的问题。 -S

这是创建多个标记的代码(大部分来自google developer网站)。 它可以正常工作,并为每个标记创建一个自定义图标。 单击时,每个标记应播放不同的音频文件(目前只有最后创建的标记可以播放)。 我还想在播放音频文件时更改图标。 我正在使用javascript和声音管理器2播放音频-但我感兴趣的是:如何引用数组中的每个标记,以便可以播放分配给该特定标记的特定音频文件?

我希望csn在没有XML和数据库的情况下执行此操作。 -Sabine

以下是相关代码:

  setMarkers(map, beaches);
}


var beaches = [
  ['Devotion', 40.710431,-73.948432, 0],
 ['Tester', 40.711223,-73.958416, 1],
];

function setMarkers(map, locations) {

  var image = new google.maps.MarkerImage('biker.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(32, 32),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 32)
      );

 var newimage = new google.maps.MarkerImage('biker_click.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(32, 32),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(0, 32)
      );

var shape = {
    coord: [1, 1, 1, 20, 18, 20, 18 , 1],
   type: 'poly'
  };

  for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shape: shape,
        title: beach[0],
        zIndex: beach[3],

    });
  }



function markerClick() {
    console.log('click');
  }
  google.maps.event.addListener(marker, 'click', markerClick);

function markerClick() {
  var playing = sm2.toggle('http://mm1.ellieirons.com/wp-content/uploads/2012/03/beeps_bubbles.mp3', true);
  if (playing) {
    this.setIcon(newimage);
  } else {
    this.setIcon(image);
  }
}

假设您有一个网址数组:

var sounds = ["http://mm1.ellieirons.com/wp-content/uploads/2012/03/beeps_bubbles.mp3",
              "http://mm1.ellieirons.com/wp-content/uploads/2012/03/beeps_bubbles2.mp3"];

然后,您可以尝试执行以下操作:

 for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
       position: myLatLng,
       map: map,
       icon: image,
       shape: shape,
       title: beach[0],
       zIndex: beach[3]
    });
    marker.sound = sounds[i];  //Storing associated sound in marker
    google.maps.event.addListener(marker, 'click', markerClick);
 }

并将处理程序修改为此:

function markerClick() {
   var playing = sm2.toggle(this.sound, true);
   if (playing) {
       this.setIcon(newimage);
   } else {
       this.setIcon(image);
   }
}

像这样

var beaches = [
 ['Devotion', 40.710431,-73.948432, 0, 'sound1.mp3'],
 ['Tester', 40.711223,-73.958416, 1, 'sound2.mp3'],
];

// ...

  for (var i = 0; i < locations.length; i++) {
    var beach = locations[i];
    var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
    var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        icon: image,
        shape: shape,
        title: beach[0],
        zIndex: beach[3],

    });
    google.maps.event.addListener(marker, 'click', function() {
       playSound(beach[4]);
    });
  }

因为您在循环内定义了一个匿名函数,所以它可以访问循环的变量(这是一个“关闭”)。 您将声音的名称添加到与海滩关联的数据中,然后将其名称从闭包传递到实际上导致其播放的函数(我称其为playSound)。

希望这会有所帮助,但这不是一个完整的食谱。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM