Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google maps 谷歌地图API-查找导致错误的航路点_Google Maps_Error Handling - Fatal编程技术网

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

Google maps 谷歌地图API-查找导致错误的航路点,google-maps,error-handling,Google Maps,Error Handling,我们一直在开发一个ASP.NET应用程序,该应用程序与Google Maps API配合使用,以帮助我们地区的一家小型航运公司进行物流和规划。其中一个功能是该公司输入一份客户名单,系统将把所有客户地址打包成一系列的航路点,发送给谷歌,并获取路线的方向信息 我们一直面临的一个问题是,我们数据库中的许多地址都不是很好,而且谷歌地图常常无法处理这些地址。当我们这样做时,我们会收到一个错误代码,但我希望能够确定哪个航路点失败(这样客户端就可以去修复数据库中的地址) 编辑以下是一段处理初始化和当前错误处理

我们一直在开发一个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.");

        }
函数初始化(){
如果(GBrowserIsCompatible()){
map=新的GMap2(document.getElementById(“map”);
addControl(新的gsmallmappcontrol());
gdir=新的GDirections(map);
addListener(gdir,“load”,onGDirectionsLoad);
addListener(gdir,“错误”,handleErrors);
if(document.getElementById(“”).getAttribute(“值”)!=“”)
{
设置方向();
}
}
}
函数setDirections(){
var waypoints=新数组();
var str=document.getElementById(“”).getAttribute(“值”);
航路点=document.getElementById(“”).getAttribute(“值”).split(“”):“”;
loadFromWaypoints(waypoints,{getSteps:true});
}
函数句柄错误(){
if(gdir.getStatus().code==G\u GEO\u UNKNOWN\u ADDRESS)
警报(“找不到其中一个指定地址的相应地理位置。这可能是因为该地址相对较新,或者可能不正确。\n错误代码:“+gdir.getStatus().code”);
else if(gdir.getStatus().code==G\u GEO\u SERVER\u ERROR)
警报(“无法成功处理地理编码或方向请求,但不知道失败的确切原因。\n错误代码:“+gdir.getStatus().code”);
else if(gdir.getStatus().code==G\u GEO\u MISSING\u QUERY)
警报(“HTTP q参数丢失或没有值。对于geocoder请求,这意味着指定了一个空地址作为输入。对于directions请求,这意味着在输入中未指定任何查询。\n错误代码:“+gdir.getStatus().code”);
else if(gdir.getStatus().code==G\u GEO\u BAD\u KEY)
警报(“给定的密钥无效或与给定的域不匹配。\n错误代码:“+gdir.getStatus().code”);
else if(gdir.getStatus().code==G\u GEO\u BAD\u请求)
警报(“无法成功解析方向请求。\n错误代码:“+gdir.getStatus().code”);
else警报(“发生未知错误”);
}
问题是GDirections对象中的LoadFromWayPoints()方法似乎不会基于每个单独的航路点返回状态,而是仅从整个列表返回状态(如果一个失败,则所有都会失败)


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

如果您使用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);
}
函数日志地址(响应)
{
如果(!response | | response.Status.code!=200)
{
//记录response.name和response.Status.code
返回;
} 
//否则一切都很好
}
对于(变量i=0;i
他们是,但我猜当你得到一个602未知地址时,你想通知用户

编辑:

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

  • 您可以在loadFromWaypoints请求之前执行getLocation请求(如您所建议的)然后使用为每个地址返回的纬度和经度数据作为loadFromWayPoints的参数。这会将地理编码处理从loadFromWayPoints请求中分离出来,但会为每个地理编码查找添加额外的往返行程。当用户第一次输入地址时,这确实是您只想执行一次的操作(请参阅下一步)
  • 当用户第一次输入地址信息时,您可以同时执行GClientGeocoder getLocations查找,并获取纬度、经度以及地址存储在数据库中。这样,如果用户输入的地址无法进行地理编码,则您可以要求他们重新输入地址,或者让他们选择谷歌地图上的位置(并从中获取lat和lng)。这并不能解决您现在拥有的地址数据的问题,但也许您可以编写一些代码来运行现有数据(脱机),并在数据库中标记无法进行地理编码的地址
您的编辑(抱歉,我刚刚看到)提供了关于我的特定问题的更好的信息。您提出的两点正是我一直在想的(关于如果我