假設我有一個div
內的地圖,寬度爲x
,高度爲y
,縮放等級爲t。我怎樣才能找到當前可見的地圖的面積,寬度和高度?如何獲取當前地圖的當前區域,寬度和高度(相對於經度和緯度)?
1
A
回答
9
下面是答案,
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);
-1
<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>
和UR定義類似於烏爾HTML組件...
<input id="searchTextField" type="text" size="50"> <div id="map_canvas"></div>
添加下面一行行頭.. <script src="//maps.googleapis.com/maps/api/js?sensor=false&libraries=places" type="text/javascript"></script>
編輯 你可以得到高度和寬度從google.maps對象..我無法改變你的要求的代碼。但我相信這會給你一個好主意。
好運
+1
你是從哪裏複製粘貼此。 – defau1t 2012-03-02 06:01:42
+0
寬度,高度,可用地圖面積在哪裏? – user960567 2012-03-02 06:01:47
相關問題
- 1. 如何獲取黑莓當前位置的經度和緯度?
- 2. 如何獲取Android中當前位置的緯度和經度?
- 3. 當前位置,經度和緯度
- 4. Android - 如何獲取當前位置(經度和緯度)?
- 5. 當前經緯度
- 6. 獲取當前位置的經度和緯度
- 7. 在Java中獲取用戶當前的緯度和經度
- 8. GoogleMaps API無法獲取當前的緯度和經度
- 9. 獲取當前的緯度和經度在android應用程序
- 10. 獲取當前緯度和經度在android上
- 11. 獲取當前位置經度和緯度在android
- 12. 獲取當前的經緯度
- 13. 獲取用戶當前的經/緯度
- 14. 獲取當前位置的經緯度
- 15. 如何確定我的當前位置(經度,緯度和高度)?
- 16. 如何從谷歌地圖獲取當前的經緯度
- 17. 獲取Embed Clutter Stage的當前寬度和高度
- 18. 如何確定我的當前位置(經度和緯度)?
- 19. 從Google地理定位API獲取當前經緯度和lng
- 20. 獲取緯度和經度
- 21. 獲取經度和緯度
- 22. 如何從我的android設備獲取當前的經度和緯度
- 23. 如何比較當前用戶的緯度和經度與其他用戶緯度和經度
- 24. 如何獲取WP7中仿真器設備的當前經度和緯度值?
- 25. 如何在iOS 4.0/4.2中獲取當前位置的經度和緯度?
- 26. 如何使用JAVA獲取我當前位置的經度和緯度
- 27. 獲取前一個相機谷歌地圖上的經度和緯度
- 28. 如何在Android中獲取緯度和經度或谷歌地圖的高度?
- 29. Mapkit當前位置的緯度和經度
- 30. 開放層當前位置的經度和緯度
我想說的是,當計算一條線上點之間的距離時,不需要幾何圖形庫......然後我記得我們生活在一個球體上。 #fezgame – Ziggy 2013-10-16 16:52:38
好的答案 - 請注意,在初始化map/api時,您必須明確指定要使用幾何庫。 – Victor 2016-10-10 10:57:59