问题描述
我正在尝试使用 Bing Maps Rest Toolkit 对英国邮政编码进行地理编码。该代码应对地址进行地理编码,然后将其传递到 ASP.Net 网页,在该网页中,地理编码的地址用于在地图上绘制航点。
应用程序构建没有错误,但每当我尝试运行它时,页面在显示任何内容之前超时。我推断在这一行调用 Rest API 时它失败了:
var response = await request.Execute();
除此之外,我不确定出了什么问题。我对此很陌生,因此非常感谢您的指导。
提前致谢,我的完整代码如下:
MapController.cs
public class MapController : Controller
{
// GET: Map
public ActionResult Index()
{
StopLists model = new StopLists();
var Stops = new List<Stops>()
{
new Stops (1,"Stoke Station",null,"ST4 2AA"),};
model.StopList = Stops;
foreach (var StopLocation in model.StopList)
{
Task<Location> gc = AddGeocode(StopLocation.Postcode);
StopLocation.Geocode = gc.Result;
};
return View();
}
public async Task<Location> AddGeocode(String Postcode)
{
var request = new GeocodeRequest()
{
Query = Postcode,IncludeIso2 = true,IncludeNeighborhood = true,MaxResults = 25,BingMapsKey = "Placeholder Bing Maps Key"
};
var response = await request.Execute();
//sets responce type to location type,and assigns the value to the geocode
Location Geocode = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;
return Geocode;
}
}
Index.cshtml
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<Meta charset="utf-8" />
<title></title>
</head>
<body onload="GetMap();">
<form id="form1" runat="server">
<div id="myMap" style='position:relative;width:600px;height:400px; top: 0px; left: 0px;'></div>
<div id="itineraryContainer"></div>
<script type="text/javascript">
function GetMap() {
var MapsKey = '<My Bing Maps Key>';
var map = new Microsoft.Maps.Map('#myMap',{
credentials: MapsKey,center: new Microsoft.Maps.Location(53.0146,-2.1864),zoom: 17
});
var center = map.getCenter();
getRoute(map);
getTraffic(map);
}
function getRoute(map) {
Microsoft.Maps.loadModule('Microsoft.Maps.Directions',function () {
var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
//Set Route Mode to driving
directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
@if (Model.StopList != null)
{
foreach(var StopLocation in Model.StopList)
{
@:var Stop = new Microsoft.Maps.Directions.Waypoint({
@:address:'@StopLocation.Name',location: @StopLocation.Geocode;
@:});
@:directionsManager.addWaypoint(Stop);
}
}
// Shows where the written directions will be rendered
directionsManager.setRenderOptions({ itineraryContainer: '#itineraryContainer' });
directionsManager.calculateDirections();
});
}
function getTraffic(map) {
Microsoft.Maps.loadModule('Microsoft.Maps.Traffic',function () {
var trafficManager = new Microsoft.Maps.Traffic.TrafficManager(map);
trafficManager.show();
});
}
</script>
<script type="text/javascript" src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
<p>
</p>
</form>
<p>
</p>
</body>
</html>
编辑:删除了必应地图键
解决方法
您的 AddGeocode
方法是异步的,但您从不等待它。在您的 C# 中创建一个 AddGeocode
任务数组,然后在循环之外使用 await Task.WhenAll
并行运行它们。这将有助于提高性能并确保在继续之前所有内容都已处理。
关于架构的一些其他注意事项:
- 如果您要进行地理编码的位置提前已知并且不会不断变化,请考虑提前对这些位置进行地理编码并将结果存储在数据库中。这将显着降低成本并提高性能。这是大多数位置查找类型应用的标准做法。
- 如果位置经常变化且无法进行地理编码。目前,您的代码执行来自服务器端的所有地理编码请求,然后发送结果。这意味着您的服务器上有更多的计算和更多的 http 请求,与将处理卸载到客户端相比,这将降低可扩展性。考虑在浏览器中进行地理编码调用。这意味着计算处理和那些 http 请求将由最终用户计算机发出,这将减少您服务器上的使用并允许它处理更多用户。我没有使用 Bing Maps 执行此操作的示例,但这里有一个使用 Azure Maps 的类似示例:https://azuremapscodesamples.azurewebsites.net/?sample=Methods%20for%20geocoding%20multiple%20addresses