1

我有这个问题,我在 html 中做了一些选择选项,每个选项在值中都有一个图标图像的名称:

<select onchange="function()"> // i haven't got the function yet
    <option value="green.png">green</option> 
    <option value="orange.png">orange</option> 
    <option value="teal.png">teal</option> 
</select>

而当前的javascript代码:

 <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

 <!-- Control Bar -->
 <script type="text/javascript" src="ZoomPanControl.js"></script>
 <!-- Control Bar -->

     <script type="text/javascript">
    var geocoder;
    var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-32.818044,-61.395249);
    // Map Options
  var mapOptions = {
    zoom: 15,
    center: latlng,
    disableDefaultUI: true,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
    // Map
  map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
  var ZoomPanControl = new missouristate.web.ZoomPanControl(map);
        ZoomPanControl.index = -1;
        map.controls[google.maps.ControlPosition.LEFT_TOP].push(ZoomPanControl);
}
    // Directions
function codeAddress() {
var cañada = 'Cañada de Gomez, Argentina';
var altura = document.getElementById('altura').value;
  var address = document.getElementById('address').value + altura + cañada  ;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
    // MARKER
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Error: ' + status);
    }
  });
}

// Listener 
google.maps.event.addDomListener(window, 'load', initialize);

    </script>

当用户在选择中选择一个或另一个选项时,我需要更改最后创建的标记(只是最后一个)的图标。我不知道我是否需要一个监听器,或者我可以只用“onchange”事件来做到这一点。

我该怎么做?

4

1 回答 1

4
  1. 使标记全局化
  2. 在您更改图标功能时调用 setIcon

    var marker = null;
    function codeAddress() {
      var cañada = 'Cañada de Gomez, Argentina';
      var altura = document.getElementById('altura').value;
      var address = document.getElementById('address').value + altura + cañada  ;
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          // MARKER
          marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
          });
        } else {
          alert('Error: ' + status);
        }
      });
    }
    
    <select onchange="marker.setIcon(this.value)">
      <option value="http://maps.google.com/mapfiles/ms/micons/green-dot.png">green</option> 
      <option value="http://maps.google.com/mapfiles/ms/micons/orange-dot.png">orange</option> 
      <option value="http://maps.google.com/mapfiles/ms/micons/yellow-dot.png">yellow</option> 
    </select>
    

工作示例

于 2013-08-23T01:18:15.377 回答