反向地理编码

Reverse geocoding

本文关键字:编码      更新时间:2023-09-26
setTimeout("locationplace("+latt+",+"+lonn+")",2000); //line 1
document.write("<tr><td>"+(lc+1) + "</td><td>"+sstime+"</td><td>"+ct+"</td><td>"+minsec+"</td><td><a href='#'>"+loname+"</a></td></tr>"); //line 2

反向地理代码函数如下:

function locationplace(latt,lonn)
{
    var rna;
    var llng = new google.maps.LatLng(latt,lonn);
        ligeocoder.geocode({'latLng': llng}, function(results, status)
       {
            if (status == google.maps.GeocoderStatus.OK)
            { 
                if (results[0]) 
                {
                loname=results[0].formatted_address;
                }
                else
                {
                }
            }
            else
            {
                alert("Geocoder failed due to: " + status);
            }
        });

}

我的问题是,在第1行中,名称是"(空)。我尝试使用setTimeout..但它不工作....我想显示loname..每次line1和line2执行....

我也得到错误:地理编码器失败由于:OVER_QUERY_LIMIT

请帮帮我....

  1. OVER_QUERY_LIMIT:您过于频繁地调用geocode()函数。这是在其他Stackoverflow主题中处理的(见@ over热心评论中提到的1和2)。

  2. loname空:在geocode()回调中设置loname。这个回调是异步调用的,所以你不能依赖于在调用locationplace()之后设置loname:

    locationplace(...); // calls geocode(), which sets loname asynchronously
    document.write(...loname...) // CANNOT USE HERE
    

    当您在超时时调用locationplace()时,问题甚至会被放大。

    你应该在回调中使用loname:
    if (results[0]) {                     
        loname=results[0].formatted_address;
        // use loname here:
        document.write(...loname...)
    }