我目前在使用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。我尝试使用以下hiddenfield,autopostback,session,但似乎没有任何效果,也许我做错了什么。
<%--<!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>--%>
我对googlemaps有点陌生,因为我从未尝试过,但是如果您想将代码列表从代码返回到javascript单击按钮,那么如何进行jquery ajax
调用并以的形式返回列表json
呢?您可以success
在ajax
通话的一部分中使用此列表。
这里有一个很好的例子。
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句