0

目前正在构建一个地图应用程序,从数据库中读取点。

这个级别的 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());
}
}
4

1 回答 1

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
于 2012-07-06T14:24:28.117 回答