0

オートコンプリート入力ボックスで場所が変わったときに地図を表示するコードを書きました。しかし、私はイベントをからシフトするのに問題があります

google.maps.event.addListener(autocomplete, 'place_changed', function() {}

ボタンクリックに。addListenerつまり、ボタンクリックで使用できますか。

HTML部分は次のとおりです。

<body onload="initialize()">
    <input id="searchTextField" type="text" size="50">
    <input type="button" id="Go" value="Go"/>
    <div id="map_canvas"></div>
</body>

そして、ここにJavascriptコードがあります:

function initialize(){
  var mapOptions = {
    center: new google.maps.LatLng(-33.8688, 151.2195),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(
    document.getElementById('map_canvas'),
    mapOptions);
  var input = document.getElementById('searchTextField');
  var autocomplete = new google.maps.places.Autocomplete(input);
  autocomplete.bindTo('bounds', map);
  var go = document.getElementById('Go');
  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    infowindow.close();
    var place = autocomplete.getPlace();
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(15);
    }
  var image = new google.maps.MarkerImage(
    place.icon,
    new google.maps.Size(71, 71),
    new google.maps.Point(0, 0),
    new google.maps.Point(17, 34),
    new google.maps.Size(35, 35));
  marker.setIcon(image);
  marker.setPosition(place.geometry.location);
  var address = '';
  if (place.address_components) {
    address = [(place.address_components[0] &&
      place.address_components[0].short_name || ''),
    (place.address_components[1] &&
      place.address_components[1].short_name || ''),
    (place.address_components[2] &&
      place.address_components[2].short_name || '')
    ].join(' ');
  }
  infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
  infowindow.open(map, marker);
});
}
4

2 に答える 2

0

document.getElementByid( "Go")。addEventListener( "onclick"、yourFunction、false);

yourFunctionは、例のようにラムダ関数にすることも、名前付き関数(単にその名前)への参照にすることもできます。

于 2012-05-31T08:54:09.377 に答える
0

終わり。コメントアウトするだけでいいinfowindow.close();

コード:

<script type="text/javascript">
var map;
var infowindow;
var autocomplete;
var place;
var input;

function initialize(){
var mapOptions = {
          center: new google.maps.LatLng(-33.8688, 151.2195),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);
input = document.getElementById('searchTextField');
autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
str = "strr";

infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker(
{
map: map,
}
);

function onGo(){
//infowindow.close();
place = autocomplete.getPlace();

if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(15);
}

var image = new google.maps.MarkerImage(
 place.icon,
 new google.maps.Size(71, 71),
 new google.maps.Point(0, 0),
 new google.maps.Point(17, 34),
 new google.maps.Size(35, 35));
marker.setIcon(image);
marker.setPosition(place.geometry.location);

var address = '';
if (place.address_components) {
address = [(place.address_components[0] &&
place.address_components[0].short_name || ''),
  (place.address_components[1] &&
place.address_components[1].short_name || ''),
  (place.address_components[2] &&
place.address_components[2].short_name || '')
 ].join(' ');
}

infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
}
</script>
</head>
<body onload="initialize()">
<input id="searchTextField" type="text" size="50">
<input type="button" id="Go" onclick="javascript:onGo()" value="Go"/>
<div id="map_canvas"></div>
</body>
</html>
于 2012-05-31T18:04:00.843 に答える