触发Google地图上的按钮点击问题 [英] Issue with triggering Google Maps on button click
本文介绍了触发Google地图上的按钮点击问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经编写了代码,当地点在自动填充输入框中更改时显示地图。但我无法从
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() {}
点击按钮,我们可以使用 addListener
to a button click. I mean, can we use addListener
with a button click.
以下是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代码:
And here is the Javascript code:
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);
});
}
推荐答案
完成。
只需注释 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>
这篇关于触发Google地图上的按钮点击问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文