【问题标题】:Google GeoCoding return ZERO_RESULTS谷歌地理编码返回 ZERO_RESULTS
【发布时间】:2012-09-15 04:02:55
【问题描述】:

我正在使用 google geocoding api 来获取结果,但它返回“ZERO_RESULTS”。下面是代码

public class GeoCode {

public static void main(String[] args) throws Exception {
    GeoCode.geocode("latlng=40.714224,-73.961452");
    }
private static final String URL = "http://maps.googleapis.com/maps/api/geocode/json";


public static void geocode(String address) throws Exception {

    URL url = new URL(URL + "?" + URLEncoder.encode(address, "UTF-8") + "&sensor=false");
    URLConnection conn = url.openConnection();
    ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
    IOUtils.copy(conn.getInputStream(), output);
    output.close();

    //JSONObject json = new JSONObject(output.toString());
    System.out.println(output.toString());
}
}

当我在浏览器的地址栏上手动使用地址时,它会在浏览器上显示 json 结果。但是上面的代码实际上有什么问题,它在 json 中返回“ZERO_RESULTS”?

【问题讨论】:

    标签: java google-maps-api-3 geocoding reverse-geocoding


    【解决方案1】:

    您无需使用 URLEncoder 对地址进行编码,只需:

    URL url = new URL(URL + "?" + address + "&sensor=false");
    URLConnection conn = url.openConnection();
    ByteArrayOutputStream output = new ByteArrayOutputStream(1024);
    IOUtils.copy(conn.getInputStream(), output);
    output.close();
    System.out.println(output.toString());
    

    或者更好:

       URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?" + address + "&sensor=false");
    try {
        output = new Scanner(url.openStream()).useDelimiter("\\A").next());
    } catch (java.util.NoSuchElementException e) {
        //empty result
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-27
      • 2018-10-17
      • 2018-01-17
      • 2012-04-10
      • 1970-01-01
      • 2016-03-23
      • 2014-12-14
      • 1970-01-01
      相关资源
      最近更新 更多