2010-05-28 33 views
2

我有我的标记的坐标。现在我想获得标记的地址。 所以我搜索了网页,发现google maps reserve geocode。 现在我试着做到以下几点:谷歌地图 - 保留地理编码 - >错误“无效标签”

$.getJSON('http://maps.google.com/maps/api/geocode/json?latlng='+point.lat()+','+ point.lng() +'&key='+apiKey+'&sensor=false&output=json&callback=?', 
function(data) { 
    console.log(data); 
}); 

当我试图表明的地址,这意味着获得JSON,萤火虫引发以下错误:

invalid label 

"status": "OK",\n 

我搜查了很多,但没有找到解决我的问题的答案。 你能告诉我我的代码有什么问题吗?

是否有另一种方法获取坐标的地址数据?

回答

4

您可能会被same origin policy阻止。昨天我回答了a similar question关于这个话题,你可能想看看。

当Google地图为JavaScript提供全功能Client-side Geocoding API时,您正在尝试使用Server-side Geocoding Web Service。还有一个v2 version,但最近已被弃用。

这会自动解决您的同源问题,此外,请求限制将根据客户端IP地址而不是每个服务器IP地址进行计算,这对于热门网站会产生巨大差异。

客户端服务应该很容易使用。下面是使用第3版API一个非常简单的反向地理编码例如:

<script src="http://maps.google.com/maps/api/js?sensor=false"></script> 

<script type="text/javascript">  
    var geocoder = new google.maps.Geocoder(); 
    var point = new google.maps.LatLng(51.50, -0.12); 

    if (geocoder) { 
     geocoder.geocode({ 'latLng': point }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      if (results[1]) { 
       console.log(results[1].formatted_address); 
      } 
      else { 
       console.log('No results found'); 
      } 
     } 
     else { 
      console.log('Geocoder failed due to: ' + status); 
     } 
     }); 
    }  
</script> 

上面的例子应该打印Lambeth, Greater London SE1 7, UK到控制台。

1

非常感谢我寻找的答案。我认为这里需要做一些小改动,results[1]应该是results[0],否则会显示“邻居”的地址。完整的应该是这样的,

<script src="http://maps.google.com/maps/api/js?sensor=false"></script> 

<script type="text/javascript">  
    var geocoder = new google.maps.Geocoder(); 
    var point = new google.maps.LatLng(23.7442812,90.3728129); 

    if (geocoder) { 
     geocoder.geocode({ 'latLng': point }, function (results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      if (results[0]) { 
       console.log(results[0].formatted_address); 
      } 
      else { 
       console.log('No results found'); 
      } 
     } 
     else { 
      console.log('Geocoder failed due to: ' + status); 
     } 
     }); 
    }  
</script> 

输出将在控制台中显示的,Road No 8A, Dhaka, Bangladesh