代码之家  ›  专栏  ›  技术社区  ›  TheTXI

谷歌地图API-查找导致错误的航路点

  •  2
  • TheTXI  · 技术社区  · 17 年前

    我们一直在开发一个ASP.NET应用程序,该应用程序与Google Maps API配合使用,以帮助我们地区的一家小型航运公司进行物流和规划。其中一个功能是该公司输入一份客户名单,系统将把所有客户地址打包成一系列的航路点,发送给谷歌,并获取路线的方向信息。

    以下是处理初始化和当前错误处理的代码块:

    function initialize() {
              if (GBrowserIsCompatible()) {      
                map = new GMap2(document.getElementById("map"));
                map.addControl(new GSmallMapControl());
                gdir = new GDirections(map);
                GEvent.addListener(gdir, "load", onGDirectionsLoad);
                GEvent.addListener(gdir, "error", handleErrors);
    
                if (document.getElementById("<%=hiddenWayPoints.ClientID %>").getAttribute("value") != '')
                {
                    setDirections();
                }
    
              }
            }
    
    
            function setDirections() {
                var waypoints = new Array();
                var str = document.getElementById("<%=hiddenWayPoints.ClientID%>").getAttribute("value");
    
                waypoints = document.getElementById("<%=hiddenWayPoints.ClientID %>").getAttribute("value").split(":");
    
                gdir.loadFromWaypoints(waypoints, {getSteps:true});
    
            }
    
            function handleErrors(){
               if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
                 alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
               else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
                 alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    
               else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
                 alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    
               else if (gdir.getStatus().code == G_GEO_BAD_KEY)
                 alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    
               else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
                 alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    
               else alert("An unknown error occurred.");
    
            }
    

    问题是GDirections对象中的LoadFromWayPoints()方法似乎不会基于每个单独的航路点返回状态,而是仅从整个列表返回状态(如果一个失败,则所有都会失败)。

    我能想到的唯一真正的解决方案(不需要在系统的其他区域执行检查)是在单独的请求中将每个航路点发送到Google Maps,并在发送GDirections对象的整个航路点列表之前以这种方式检查其有效性,但这似乎效率极低(尤其是在处理更大范围的客户地点时)。

    1 回复  |  直到 17 年前
        1
  •  2
  •   RedBlueThing    17 年前

    如果您使用GClientGeocoder对象执行请求,那么您将为每个getLocations调用返回一个适当的响应代码:'

    function logAddress (response)
    {
        if (!response || response.Status.code != 200) 
        {
            // log the response.name and the response.Status.code
            return;
        } 
    
        // otherwise everything was fine
    }
    
    for (var i = 0; i < addresses.length; i++)
    {
        var geocoder = new GClientGeocoder ();
        geocoder.getLocations (addresses[i], logAddress);
    }
    

    他们是 various response codes

    是的,对于整个方向请求,loadFromWaypoints只会收到一次错误回调。您所做的不仅仅是一个简单的地理编码请求,实际上您正在为一系列地址值生成方向并渲染地图覆盖。我提出了几个解决方案:

    • 当用户第一次输入地址信息时,您可以在此时执行GClientGeocoder getLocations查找,并获取纬度、经度和地址一起存储在数据库中。这样,如果用户输入的地址无法进行地理编码,那么您可以要求他们重新输入地址,或者让他们在谷歌地图上选择位置(并从中获取lat,lng)。这并不能解决您现在拥有的地址数据的问题,但也许您可以编写一些代码来运行现有数据(脱机),并在数据库中标记无法进行地理编码的地址。
    推荐文章