[英]how to add Places search box and text directions with setPanel() together in javascript, google map
[英]How to add places search box to a map?
我對在Google地圖中實現Google Maps搜索框感興趣。 如何在此地圖代碼中添加地點搜索框:
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<div id="map_canvas" style="width: 700px; height: 600px; float: left; margin: 10px"></div>
<script type="text/javascript">
function InitializeMap() {
var latlng = new google.maps.LatLng(32, 35);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP };
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
window.onload = InitializeMap;
</script>
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
var geocoder;
var map;
function InitializeMap() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(32, 35);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function searchAddress() {
var address = document.getElementById("address").value;
geocoder.geocode(
{'address': address}, function(results, status){
if(status === google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
document.getElementById('address').value = results[0].formatted_address;
}
}
);
}
//window.onload = InitializeMap;
google.maps.event.addDomListener(window, 'load', InitializeMap);
</script>
</head>
<body>
<div>
<input id="address" type="text" name="address" value="">
<input type="button" value="Search" onclick="searchAddress()">
</div>
<div id="map_canvas" style="width:400px; height:400px;"></div>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.