2015-02-24 32 views
0

,我收到了谷歌地圖的當前範圍擴大的LatLngBounds盒致電:如何使用經緯度數據

map.getBounds() 

這將返回的LatLngBounds。例如:

((-26.574013562724005, -1.5375947819336488), (-25.230016040794602, 3.735842718066351)) 

是否有可能得到新的經緯度點,如果我想圍繞地圖的中心百分比(例如1%),以增加地圖界限。

我試着乘以1.01的每個座標,但是這會改變地圖而不會增加邊界。

我想使用增加的框大小來啓動緩存標記,以便在用戶平移或縮小時保存對服務器的調用。

下圖將會更好地解釋需要採取什麼措施:

enter image description here

+0

簡單的方法是通過減小縮放級別一個('map.setZoom(map.getZoom() - 1)'),除非你只是試圖繪製一個代表地圖上邊界的框。 – geocodezip 2015-02-24 19:55:21

+0

我不想減小縮放級別,我想開始加載緩存當前範圍外的標記。 – Morne 2015-02-24 19:57:14

+0

請在你的問題中澄清。 – geocodezip 2015-02-24 19:58:31

回答

2

基於markerclusterergetExtendedBounds功能,你可以用這一個:

function getExtendedBounds(map, overlay) { 
    //With _mapBoundsEnlargePixels we add some extra space surrounding the map 
    //change this value until you get the desired size 
    var _mapBoundsEnlargePixels = 500; 

    var projection = overlay.getProjection(); 
    var bounds = angular.copy(map.getBounds()); 

    // Turn the bounds into latlng. 
    var tr = new google.maps.LatLng(bounds.getNorthEast().lat(), 
     bounds.getNorthEast().lng()); 
    var bl = new google.maps.LatLng(bounds.getSouthWest().lat(), 
     bounds.getSouthWest().lng()); 

    // Convert the points to pixels and extend out 
    var trPix = projection.fromLatLngToDivPixel(tr); 
    trPix.x = trPix.x + _mapBoundsEnlargePixels; 
    trPix.y = trPix.y - _mapBoundsEnlargePixels; 

    var blPix = projection.fromLatLngToDivPixel(bl); 
    blPix.x = blPix.x - _mapBoundsEnlargePixels; 
    blPix.y = blPix.y + _mapBoundsEnlargePixels; 

    // Convert the pixel points back to LatLng 
    var ne = projection.fromDivPixelToLatLng(trPix); 
    var sw = projection.fromDivPixelToLatLng(blPix); 

    // Extend the bounds to contain the new bounds. 
    bounds.extend(ne); 
    bounds.extend(sw); 

    return bounds; 
} 
+0

我最終採取了一種完全不同的方法,但這個答案仍然有用。謝謝! – Morne 2015-02-26 08:07:31

+1

如果你分享了你的方法,那將會很棒。 – 2016-10-05 13:34:56

+0

是的,會對你的方法感興趣,@Morne – xsonic 2016-11-17 15:30:10