【问题标题】:How to display markers from select option values?如何从选择的选项值中显示标记?
【发布时间】:2023-04-05 17:28:01
【问题描述】:

仅在地址变量上工作的代码。 所以在选择选项值有多个地址。

function geocodeAddress(geocoder, resultsMap) {   
                var address = document.getElementById('Select').value;   
                 for(i = 0; i < address.length;i++){   
                 geocoder.geocode({'address': address[i]}, function(results, status) {
                            if (status === google.maps.GeocoderStatus.OK) {
                             // resultsMap.setCenter(results[0].geometry.location);


                              var marker = new google.maps.Marker({
                                map: resultsMap,
                                position: results[i].geometry.location
                              });
                              markers.push(marker);
                            } else {
                              alert('Geocode was not successful for the following reason: ' + status);
                            }   
                        });   
                       }
                    }

【问题讨论】:

标签: google-maps-api-3 option


【解决方案1】:

根据相关问题修改: Google Maps : change map marker location on dropdown

var geocoder;
var map;
var geoMarker;
var bounds = new google.maps.LatLngBounds();

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  geoMarker = new google.maps.Marker();
  geoMarker.setPosition(map.getCenter());
  geoMarker.setMap(map);
  var txtArray = $("#location option").each(function() {
    console.log($(this).text());
    geocodeAddress($(this).text());
  });
  $("#location").change(function() {
    var addr = ($('#location :selected').text());
    console.log(addr);
    var geocoder = new google.maps.Geocoder();
    geocoder.geocode({
      'address': addr
    }, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[0].geometry.viewport) {
          map.fitBounds(results[0].geometry.viewport);
        } else if (results[0].geometry.bounds) {
          map.fitBounds(results[0].geometry.bounds);
        } else {
          map.setCenter(results[0].geometry.location);
        }
      } else {
        alert("Error geocoding address: " + address + "status=" + status);
      }
    });
  });

}
google.maps.event.addDomListener(window, "load", initialize);

function geocodeAddress(address) {
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var marker = new google.maps.Marker({
        position: results[0].geometry.location,
        map: map,
        title: address
      });
      bounds.extend(results[0].geometry.location);
      map.fitBounds(bounds);
    } else {
      alert("Error geocoding address: " + address + "status=" + status);
    }
  });
}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<select id="location">
  <option>New York, NY</option>
  <option>Newark, NJ</option>
  <option>Philadelphia, PA</option>
</select>
<div id="map_canvas"></div>

【讨论】:

  • 但在清除标记不起作用时。标记.push(标记);
  • 问题中没有clearMarkers,不是你的意思。
猜你喜欢
  • 1970-01-01
  • 2018-11-29
  • 2021-11-07
  • 2019-05-28
  • 1970-01-01
  • 2015-08-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多