2012-03-20 61 views
2

我使用bing地圖ajax v7,爲了簡單起見,我們假設我在世界各地放置了10個PinPoints。我試圖將地圖縮放到最低級別,以便最接近的PinPoint對用戶的當前位置仍然可見。如果有人能指出我正確的方向,我將不勝感激。bing地圖:如何設置縮放級別,以便用戶可以看到精確位置當前位置

$(document).ready(function() { 
     var windowHeight = $(window).height(); 
     var windowWidth = $(window).width(); 

     map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), { 
      credentials: "myCredentials", 
      backgroundColor: "#A4C4ED", 
      zoom: 3, 
      height: windowHeight, 
      width: windowWidth 
     }); 
     Microsoft.Maps.Events.addHandler(map, 'viewchange', hideInfoBox); 
     Microsoft.Maps.Events.addHandler(map, 'click', hideInfoBox); 

     //get users location and set view bound 
     var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map); 
     var viewRectangle = Microsoft.Maps.LocationRect(geoLocationProvider.getCurrentPosition()); 
     map.setView({ bounds: viewRectangle }); 

     dataLayer = new Microsoft.Maps.EntityCollection(); 
     map.entities.push(dataLayer); 

     var infoboxLayer = new Microsoft.Maps.EntityCollection(); 
     map.entities.push(infoboxLayer); 

     //create initial infobox 
     infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), { 
      visible: false, 
      offset: new Microsoft.Maps.Point(0, 20) 
     }); 
     infoboxLayer.push(infobox); 

     Microsoft.Maps.loadModule('Microsoft.Maps.Search', { callback: searchModuleLoaded }); 
    }); 

回答

3

我假設你是一個針點,並且你有另外10個針點位於地圖上。

首先你需要找到最接近你的精確點。 您可以使用此函數來預測包含經度和緯度的兩個位置對象。

oLocation1 = {latitude:0,longitude:0}; 
oLocation2 = {latitude:19,longitude:23}; 


function calcDistHaversine (oLocation1, oLocation2) { 
     var dLat = (oLocation2.latitude * Math.PI/180 - oLocation1.latitude * Math.PI/180);//*Math.PI*180; 
     var dLon = (oLocation2.longitude * Math.PI/180 - oLocation1.longitude * Math.PI/180);//*Math.PI*180; 
     var lat1 = oLocation1.latitude * Math.PI/180;//*Math.PI*180; 
     var lat2 = oLocation2.latitude * Math.PI/180;//*Math.PI*180; 

     var a = Math.sin(dLat/2) * Math.sin(dLat/2) + 
       Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
     var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
     var distance = 6371 * c; 
     return distance; 
    }; 

因此,您將得到這兩個位置之間的距離,就地球曲率而言。

現在你有你的位置和最接近的精確位置。

讓我們將它們命名爲您的,它們。

接下來,您需要創建包含轉換爲Microsoft位置對象的這兩個位置的數組。

var yourLocation= new Microsoft.Maps.Location(your.latitude, your.longitude); 
    var theirLocation= new Microsoft.Maps.Location(their.latitude, their.longitude); 

    var arr = []; 
    arr.push(yourLocation); 
    arr.push(theirLocation); 

現在,您可以使用bing地圖功能,根據給定位置爲您提供最佳縮放和指向。

var bestView = Microsoft.Maps.LocationRect.fromLocations(arrLocations); 

然後根據我們找到的最佳視圖設置地圖視圖。

setTimeout((function() { 
     map.setView({ bounds: bestView }); 
    }).bind(this), 1000); 
+0

爲什麼需要'setTimeout'功能?我沒有超時使用它,地圖不會縮放。按照此處所述添加了超時,並且工作正常 – sebagomez 2014-07-27 17:31:41

+0

所有人員發生同步,因此1秒鐘是一個粗略緩衝區,直到所有引腳實際都由地圖引擎繪製爲止。 只有這樣你才能使setView具有邊界。 – 2014-08-04 06:15:10

+0

好吧,但沒有一個事件我可以把'setView'代碼,所以我不必等待?它似乎脆弱 – sebagomez 2014-08-05 03:20:40

相關問題