2016-11-28 10 views
-1

この問題はどうかお手伝いできますか? Googleマップを使用していますが、別の選択オプションを選択してもマーカーが変わらないという問題があります。私はちょうど別の場所の配列を選択できるようにしたいとすぐに表示する必要があります。別のオプションを選択したときに地図の場所を変更したくない場合

<!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> 
+0

'$( 'location')'は '$( '#location')' – geocodezip

答えて

1

希望、以下のコードは、あなたのニーズを満たすだろう:私はあなたのコードにいくつかの変更を行った

は、ここに私のコードです。

<!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> 
関連する問題