按下按钮时触发Google地图的问题

时间:2012-05-31 08:42:40

标签: javascript html google-maps

我编写了代码,当地点在自动填充输入框中更改时显示地图。但我无法从

转移事件
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);
});
}

2 个答案:

答案 0 :(得分:0)

document.getElementByid(“Go”)。addEventListener(“onclick”,yourFunction,false);

yourFunction可以是示例中的lambda函数,也可以是指定函数的引用(只是它的名称)

答案 1 :(得分: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>