我选择其他选项时,地图位置不想更改

时间:2016-11-28 13:12:32

标签: javascript jquery google-maps-api-3

嘿伙计们能帮助我解决这个问题吗? 我使用谷歌地图,问题是当我选择不同的选择选项时标记不会改变。我只是希望能够选择不同的位置数组,并且必须立即显示。

这是我的代码:

<!DOCTYPE html>
<html> 
<head> 
  <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
  <title>Google Maps Multiple Markers</title> 
  <script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>
</head> 
<body>
  <div id="map" style="width: 500px; height: 400px;"></div>
  <select id="location">
  <option>locations1</option>
 <option>locations2</option>
 </select>

  <script type="text/javascript">

$('location').change(function(){

var location = $('location').val();

if(location == "locations1"){
 var locations = [
      ['Z Beach', -33.890542, 151.274856, 4],
      ['Y Beach', -33.923036, 151.259052, 5],
      ['X Beach', -34.028249, 151.157507, 3],
      ['T Beach', -33.80010128657071, 151.28747820854187, 2],
      ['V', -33.950198, 151.259302, 1]
    ];
}

if(location == "locations2"){
 var locations = [
      ['C Beach', -20.890542, 140.274856, 4],
      ['B Beach', -20.923036,140.259052, 5],
      ['E Beach', -20.028249, 140.157507, 3],
      ['A Beach', -20.80010128657071, 140.28747820854187, 2],
      ['C Beach', -20.950198, 140.259302, 1]
    ];
}


})


    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(-33.92, 151.25),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

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

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  </script>
</body>
</html>

1 个答案:

答案 0 :(得分:3)

希望,以下代码可满足您的需求:     我对您的代码进行了一些更改。

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Google Maps Multiple Markers</title>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<select id="location">
    <option value="locations1">locations1</option>
    <option value="locations2">locations2</option>
</select>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">
    $('#location').change(function(){
        var location = $('#location').val();
        if(location == "locations1"){
            var locations = [
                ['Z Beach', -33.890542, 151.274856, 4],
                ['Y Beach', -33.923036, 151.259052, 5],
                ['X Beach', -34.028249, 151.157507, 3],
                ['T Beach', -33.80010128657071, 151.28747820854187, 2],
                ['V', -33.950198, 151.259302, 1]
            ];
        }
        if(location == "locations2"){
            var locations = [
                ['C Beach', -20.890542, 140.274856, 4],
                ['B Beach', -20.923036,140.259052, 5],
                ['E Beach', -20.028249, 140.157507, 3],
                ['A Beach', -20.80010128657071, 140.28747820854187, 2],
                ['C Beach', -20.950198, 140.259302, 1]
            ];
        }
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: new google.maps.LatLng(-33.92, 151.25),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
        var infowindow = new google.maps.InfoWindow();
        var marker, i;
        var bounds = new google.maps.LatLngBounds();
        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map
            });
            bounds.extend(marker.position);
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
        map.fitBounds(bounds);
    })
</script>
</body>
</html>