2012-07-06 33 views
0

目前正在構建一個地圖應用程序,從數據庫中讀取點。谷歌地圖 - 來自VB.Net的JSON標記

剛接觸這個級別的Javascript,現在有點失落了。

從SQL數據源在VB.Net中創建一個Json。

任何人都可以幫助編輯我的代碼,因爲沒有創建新的地圖,標記添加到現有的?我所做的任何編輯只是不加分。

VB.Net

Dim markers As New List(Of String) 
Dim nearbyLocations = CType(sqlData.Select(DataSourceSelectArguments.Empty), DataView) 
For Each location As DataRowView In nearbyLocations 
    markers.Add(String.Format("{{ title: ""Name {0}"", position: new google.maps.LatLng({1}, {2}) }}", location("AccName"), location("Latitude"), location("Longitude"))) 
Next 

Dim locations = "[" & String.Join(",", markers.ToArray()) & "]" 
ClientScript.RegisterStartupScript(Me.GetType(), "LoadMap",_ 
    String.Format("init_map('map', {0}, {1}, 13, {2});", lat, lng, locations), True) 

腳本

function init_map(map_canvas_id, lat, lng, zoom, markers) { 
var myLatLng = new google.maps.LatLng(lat, lng); 

var options = { 
    zoom: zoom, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 


map = new google.maps.Map(document.getElementById(map_canvas_id), options); 


if (markers && markers.length > 0) { 
    var bounds = new google.maps.LatLngBounds(); 

    for (var i = 0; i < markers.length; i++) { 
     var marker = new google.maps.Marker(markers[i]); 
     marker.setMap(map); 

     bounds.extend(marker.getPosition()); 
    } 

    map.fitBounds(bounds); 
    map.setCenter(bounds.getCenter()); 
} 
} 

回答

0

在後面的代碼:

Dim markers As New List(Of String) 
Dim nearbyLocations = CType(sqlData.Select(DataSourceSelectArguments.Empty), DataView) 
For Each location As DataRowView In nearbyLocations 
    markers.Add(" { ""title"" : """ & location("AccName") & """, ""lat"" : " & location("Latitude") & ", ""long"" : " & location("Longitude") & " } ") 
Next 

Dim locations = "[" & String.Join(",", markers.ToArray()) & "]" 
ClientScript.RegisterStartupScript(Me.GetType(), "LoadMap",_ 
    String.Format("init_map('map', {0}, {1}, 13, {2});", lat, lng, locations), True) 

在aspx頁面:

//<HTML> stuff.... 
//... 
//... 

var map; 

function init_map(map_canvas_id, lat, lng, zoom, markers) { 
var myLatLng = new google.maps.LatLng(lat, lng); 

var options = { 
    zoom: zoom, 
    center: myLatLng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
}; 

map = new google.maps.Map(document.getElementById(map_canvas_id), options); 

var positions = [<% Response.Write(String.Join(",", markers.ToArray())) %>];  
var marker; 
var curPosition; 
for (var i = 0; i < positions.length; i++) { 
    curPosition = positions[i]; 
    marker = new google.maps.Marker({ 
     map: map, 
     position: new google.maps.LatLng(curPosition.lat,curPosition.long), 
     title: curPosition.title 
    }); 
} 

//..</HTML> stuff 
+0

感謝。如何在VB中引用腳本執行? – user1506609 2012-07-06 14:28:20

+0

我想你已經繪製了一張地圖,並且你想添加新的標記。它如何得到你的「新」標記列表?我會做的是做一個jQuery的Ajax調用一個Web服務,返回一個JSON。 – 2012-07-06 15:09:08

+0

通過基於輸入字段的按鈕單擊VB從SQL數據源檢索新標記。之前沒有用過Jquery,所以去了VB/Javascript路線。想法是用戶可以更改參數,標記將根據這些添加。 – user1506609 2012-07-06 15:23:03