[英]Issue with triggering Google Maps on button click
I have written code which displays the map when the place changes in an autocomplete input box. 我编写了在自动完成输入框中更改地点时显示地图的代码。 But I'm having trouble shifting the event from
但是我无法将事件从
google.maps.event.addListener(autocomplete, 'place_changed', function() {}
to a button click. 单击按钮。 I mean, can we use
addListener
with a button click. 我的意思是,是否可以通过单击按钮使用
addListener
。
Here is the HTML part: 这是HTML部分:
<body onload="initialize()">
<input id="searchTextField" type="text" size="50">
<input type="button" id="Go" value="Go"/>
<div id="map_canvas"></div>
</body>
And here is the Javascript code: 这是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);
});
}
document.getElementByid("Go").addEventListener("onclick", yourFunction, false); document.getElementByid(“ Go”)。addEventListener(“ onclick”,yourFunction,false);
yourFunction can be either the lambda function as in your example, or a referenc to a named function (simply it's name) yourFunction可以是示例中的lambda函数,也可以是对命名函数的引用(简称为它的名称)
Done. 做完了 Just needed to comment out
infowindow.close();
只需注释掉
infowindow.close();
Code: 码:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.