Javascript 如何获取当前地图的当前面积、宽度和高度(相对于纬度和经度)?
假设我在一个div中有一张地图,宽度为x,高度为y,缩放级别为t。如何找到当前可见地图的面积、宽度和高度Javascript 如何获取当前地图的当前面积、宽度和高度(相对于纬度和经度)?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,假设我在一个div中有一张地图,宽度为x,高度为y,缩放级别为t。如何找到当前可见地图的面积、宽度和高度 <script type="text/javascript"> function initialize() { var mapOptions = { center: new google.maps.LatLng(58, 60), zoom: 13, mapTypeId: google.maps.MapTypeId.ROADMAP
<script type="text/javascript">
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(58, 60),
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 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(17); // Why 17? Because it looks good.
}
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);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
和您定义的HTML组件类似于
将以下行添加到标题中。。
编辑
您可以从google.maps对象获取高度和宽度。。我无法根据您的要求修改代码。但我相信这会给你一个好主意
祝你好运
和您定义的HTML组件类似于
将以下行添加到标题中。。
编辑
您可以从google.maps对象获取高度和宽度。。我无法根据您的要求修改代码。但我相信这会给你一个好主意
祝你好运这是答案
var spherical = google.maps.geometry.spherical,
bounds = map.getBounds(),
cor1 = bounds.getNorthEast(),
cor2 = bounds.getSouthWest(),
cor3 = new google.maps.LatLng(cor2.lat(), cor1.lng()),
cor4 = new google.maps.LatLng(cor1.lat(), cor2.lng()),
width = spherical.computeDistanceBetween(cor1,cor3),
height = spherical.computeDistanceBetween( cor1, cor4);
答案是
var spherical = google.maps.geometry.spherical,
bounds = map.getBounds(),
cor1 = bounds.getNorthEast(),
cor2 = bounds.getSouthWest(),
cor3 = new google.maps.LatLng(cor2.lat(), cor1.lng()),
cor4 = new google.maps.LatLng(cor1.lat(), cor2.lng()),
width = spherical.computeDistanceBetween(cor1,cor3),
height = spherical.computeDistanceBetween( cor1, cor4);
你从哪里复制粘贴这个。哪里是可用地图的宽度、高度、面积?你从哪里复制粘贴这个。哪里是可用地图的宽度、高度、面积?我想说的是,在计算直线上点之间的距离时,不需要几何体库。。。然后我想起我们生活在一个球体上。fezgameGreat回答-请注意,您必须明确指定在初始化地图/api时要使用几何体库。我想说的是,在计算直线上点之间的距离时,不需要几何体库。。。然后我想起我们生活在一个球体上。fezgameGreat answer-请注意,在初始化贴图/api时,必须明确指定要使用几何体库。