自动设置中心和放大谷歌地图API
automatic set center and zoom in google maps api
当我在输入框中自动完成这两个位置并单击提交时,会在两个位置之间绘制一条折线。我希望地图居中并相应地缩放,以便在最大缩放时完全可见线条。我无法弄清楚自动居中和缩放。请帮忙。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body, #map-canvas {
height: 100%;
width:100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<script>
var poly, map,place,place1;
var bounds=new google.maps.LatLngBounds();
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(-33.8688, 151.2195),
zoom: 13
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var input = /** @type {HTMLInputElement} */
(
document.getElementById('pac-input'));
var input1 = (document.getElementById('pac-input1'));
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input1);
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
var autocomplete1 = new google.maps.places.Autocomplete(input1);
autocomplete1.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map,
anchorPoint: new google.maps.Point(0, -29)
});
var marker1 = new google.maps.Marker({
map: map,
anchorPoint: new google.maps.Point(0, -29)
});
var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3
};
poly = new google.maps.Polyline(polyOptions);
google.maps.event.addDomListener(document.getElementById('submit'), 'click', function () {
marker.setVisible(true);
marker1.setVisible(true);
map.fitBounds(place.geometry.viewport);
map.setZoom(5);
// map.fitBounds(bounds);
// map.setCenter(new google.maps.LatLng(((place.geometry.location.k+place1.geometry.location.k)/2),((place.geometry.location.D+place1.geometry.location.D)/2));
poly.setMap(map);
});
google.maps.event.addListener(autocomplete, 'place_changed', function () {
infowindow.close();
marker.setVisible(false);
place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
// If the place has a geometry, then present it on a map.
if (place.geometry.viewport) {
// map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17); // Why 17? Because it looks good.
}
marker.setIcon( /** @type {google.maps.Icon} */ ({
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
}));
marker.setPosition(place.geometry.location);
// marker.setVisible(true);
poly.getPath().setAt(0, marker.getPosition());
// var myLatLng=(place.geometry.location.k,place.geometry.location.D);
//bounds.extend(myLatLng);
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);
});
google.maps.event.addListener(autocomplete1, 'place_changed', function () {
infowindow.close();
marker1.setVisible(false);
place1 = autocomplete1.getPlace();
if (!place1.geometry) {
return;
}
// If the place has a geometry, then present it on a map.
if (place1.geometry.viewport) {
//map.fitBounds(place1.geometry.viewport);
} else {
map.setCenter(place1.geometry.location);
map.setZoom(11); // Why 17? Because it looks good.
}
marker1.setIcon( /** @type {google.maps.Icon} */ ({
url: place1.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
}));
marker1.setPosition(place1.geometry.location);
//locations.push(place.geometry.location);
// marker1.setVisible(true);
poly.getPath().setAt(1, marker1.getPosition());
//var myLatLng=(place1.geometry.location.k, place1.geometry.location.D);
//bounds.extend(myLatLng);
var address = '';
if (place1.address_components) {
address = [
(place1.address_components[0] && place1.address_components[0].short_name || ''), (place1.address_components[1] && place1.address_components[1].short_name || ''), (place1.address_components[2] && place1.address_components[2].short_name || '')].join(' ');
}
// infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
// infowindow.open(map, marker1);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<input id="pac-input" type="text" placeholder="Enter a location"style="width:300px; height:30px;border-width:2px;
border-style:solid; padding-left:4px;" />
<input id="pac-input1" type="text" placeholder="Enter another location" style="width:300px; height:30px;margin-left:20px; border-width:2px;
border-style:solid; padding-left:4px;"/>
<input type="submit" class="btn btn-default"name="submit" id="submit" value="SUBMIT" style="background: rgb(162, 194, 250);
margin-top: 2px;
margin-bottom: 2px;
margin-left: 720px;
" />
<div id="map-canvas"></div>">
</body>
</html>
如果要在
提交时使地图适合折线的边界,请使用折线路径的坐标扩展空边界对象:
google.maps.event.addDomListener(document.getElementById('submit'), 'click', function () {
poly.setMap(map);
var bounds = new google.maps.LatLngBounds();
bounds.extend(poly.getPath().getAt(0));
bounds.extend(poly.getPath().getAt(1));
map.fitBounds(bounds);
});
斯菲德尔
相关文章:
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 标记的实时更新,无需加载页面谷歌地图API V3
- 覆盖在赢得'不允许点击下面的标记,谷歌地图api v3
- 谷歌地图Api和JS代码不工作
- 谷歌地图API-InfoBubble赢得't关闭-关闭()不会'不起作用
- 如何在多承诺链中处理谷歌地图API V3事件
- 谷歌地图api和gMapsLatLonPicker
- 谷歌地图API-“;toGeoJson”;返回几何体为null的对象
- 谷歌地图API v3:Initial View is Fine,but Gray Box with No Map if
- 为什么谷歌地图API's方法未从RequireJS中正确调用
- 使用谷歌地图api计算距离并将其设置在表格中<td>
- 使用谷歌地图API和phonegap显示当前位置+方向
- 使用谷歌地图api v3自动完成字段(法国大道、邮编、城市)
- 谷歌地图API标记没有标记我的位置
- 使用谷歌地图api 3为MapTypeId.TERRAIN分离按钮
- 在JQuery中使用谷歌地图Api事件/侦听器
- 在Ionic应用程序上使用$.getScript加载谷歌地图Api
- 我该如何解决“;未捕获引用错误:谷歌没有定义"?(谷歌地图API)
- 谷歌地图API-显示具有不同图标的标记
- 如何停止地图移动时,鼠标悬停在标记在谷歌地图API 3