简体   繁体   English

使用选择更改标记图标API谷歌地图v3

[英]Change Marker icon API google maps v3 with a select

I have this problem, I made some select options in html and each one has the name of an icon image in the value: 我有这个问题,我在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>

And the current javascript code: 和当前的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>

And I need to change the icon of the last created marker (just the last one) when the user selects one or another option in the select. 当用户在select中选择一个或另一个选项时,我需要更改最后创建的标记的图标(只是最后一个)。 I don't know if I need a listener or I can do it with just the "onchange" event. 我不知道我是否需要一个听众,或者我只能通过“onchange”事件来做。

How can I do it? 我该怎么做?

  1. Make the marker global 使标记全局化
  2. call setIcon on it in your change the icon function 在你更改图标功能时调用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> 

working example 工作实例

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

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