目前正在构建一个地图应用程序,从数据库中读取点。
此级别的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 :(得分: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