2013-09-23 32 views
0

我正在使用Mapbox JavaScript API(http://www.mapbox.com/mapbox.js/api/v1.3.1/),並遇到回發時加載地圖的問題。當第一次加載頁面時,我希望地圖集中在由我定義的一組座標上,但是當用戶在文本框中輸入郵編時,我希望地圖以這些座標爲中心。在我修改代碼之前,我的原始問題是地圖會加載到文檔上,並且地圖不會按照用戶在回發中定義的方式設置新的視圖,但是現在我遇到了地圖不存在的問題加載準備好的文檔,但是當用戶輸入郵編時,它可以正常工作。Postback and Document.ready

這裏是我的document.ready功能:

$(document).ready(function() { 
     var map = L.mapbox.map('map'); 
     var oldZoom = map.zoomControl; 
     oldZoom.removeFrom(map); 
     new L.Control.Zoom({ position: 'topright' }).addTo(map); 
     map.addLayer(L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png')); 
     map.setView([53.503355, -2.827564], 6); // this runs on document ready, but also sets the view on postback 
     function _doPostBack() { 
      updateMap(); 
     } // this is my attempt at a fix 
     var markerLayer = L.mapbox.markerLayer() 
     .addTo(map); 
    }); 

這裏是我想火當用戶使回傳我updateMap()函數:

function updateMap() 
    { 
     map.setView([<asp:Literal runat='server' id='currentLatSetView'/>,<asp:Literal runat='server' id='currentLongSetView'/>], <asp:Literal runat='server' id='theZoomScale'/>); 
    } 

這裏相關的按鈕代碼:

​​

所以我的問題是,我該如何添加一個檢查到我的JavaScript爲1.中心地圖上的一組座標,當頁面加載第一次和2.我如何防止setViewView()在document.ready()射擊,和而是用不同的用戶定義的座標激發不同的setView()。

回答

1

您可以定義隱藏字段以獲得您的座標值。

<asp:HiddenField runat="sever" ID="hdnXAxis" Vlaue="Your Default Value" /> 
<asp:HiddenField runat="sever" ID="hdnYAxis" Vlaue="Your Default Value" /> 

回發:

hdnXAxis.Value= txtXAxis.Text; 
hdnYAxis.Value= txtYAxis.Text; 

JS:

$(document).ready(function() { 
var xAxis = $('#<%= hdnXAxis.Client%>').val(); 
var yAxis = $('#<%= hdnYAxis.Client%>').val(); 
     var map = L.mapbox.map('map'); 
     var oldZoom = map.zoomControl; 
     oldZoom.removeFrom(map); 
     new L.Control.Zoom({ position: 'topright' }).addTo(map); 
     map.addLayer(L.tileLayer('http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png')); 
     map.setView([xAxis, yAxis], 6); // this runs on document ready, but also sets 
     var markerLayer = L.mapbox.markerLayer() 
     .addTo(map); 
    }); 

這不是測試,但這樣的事情可能會解決你的問題。