【发布时间】:2014-04-17 22:36:30
【问题描述】:
我目前是使用 asp.net 的新手。我希望使用 googlemap 在地图上显示 gps 位置列表。
我有一个在我的后端调用 web 服务的按钮事件,它返回一个坐标列表(纬度/经度)。这很好用。我的问题是如何在我的 aspx 页面中将此列表发送到 javascript。
protected void btnGetMapForAdr_Click(object sender, EventArgs e)
{
List<MultipleAddress> listofaddr;
MultipleAddress multiAddr1;
MultipleAddress multiAddr2;
MultipleAddress multiAddr3;
...
listofaddr = new List<MultipleAddress>()
{
multiAddr1,
multiAddr2,
multiAddr3
};
//Class1 is a library that contains the webservice method that returns the coordinates.
Class1 service = new Class1();
Dictionary<string, LongitudeLatitude> returnAdrListDict = service.GetMultipleLongLat(listofaddr);
List<LongitudeLatitude> newListForASPX = CreateNewList(returnAdrListDict);
List<string> listForJavascript = ConvertToListOfStrings(newListForASPX);
}
//convert dictionary to type LongitudeLatitude
private List<LongitudeLatitude> CreateNewList(Dictionary<string, LongitudeLatitude> input){
List<LongitudeLatitude> longlatListCollection = new List<LongitudeLatitude>();
foreach (KeyValuePair<string, LongitudeLatitude> item in input)
{
LongitudeLatitude adrlonglat = new LongitudeLatitude();
adrlonglat.AdressInfo = item.Key;
adrlonglat.Latitude = item.Value.Latitude;
adrlonglat.Longitude = item.Value.Longitude;
longlatListCollection.Add(adrlonglat);
}
return longlatListCollection;
}
//convert list of type LongitudeLatitude to a list of type string
private List<string> ConvertToListOfStrings(List<LongitudeLatitude> input)
{
List<string> listToReturn = new List<string>();
foreach (var item in input)
{
listToReturn.Add(item.AdressInfo.ToString() + ":" + item.Latitude.ToString() + "," + item.Longitude.ToString());
}
return listToReturn;
}
}
我找到了如何在 googlemap 中使用标记显示多个位置的示例,但我不知道如何将列表 (listToReturn) 从后面的代码发送到 aspx 页面中的 javascript。我尝试使用以下隐藏字段、自动回发、会话,但似乎没有任何效果,也许我做错了什么。
<%--<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [
['Bondi Beach', -33.890542, 151.274856, 4],
['Coogee Beach', -33.923036, 151.259052, 5],
['Cronulla Beach', -34.028249, 151.157507, 3],
['Manly Beach', -33.80010128657071, 151.28747820854187, 2],
['Maroubra Beach', -33.950198, 151.259302, 1]
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>
</body>
</html>--%>
【问题讨论】:
-
我能否获得更多关于如何解决此任务的想法。我是 asp.net 的新手,对前端(javascript...)没有太多经验
标签: javascript html asp.net google-maps