1

我们正在使用 Google 的地理定位 API 来获取 wifi 接入点的 latlong。我们看到了不一致的结果。Google 的 API 在不同的盒子上返回不同的 latlong。当我在我的开发盒上进行测试时,我得到了指向美国某个位置的 latlong。当我在 amazon ec2 盒子上进行测试时,我得到了指向日本某个位置的 latlong。其他人对 Google 的 GeoLocation API 有过同样的经历吗?
下面是来自两个主机的代码和响应字符串。两个主机上的 JDK 和 JSON jar 版本相同。

public void testWifiIdMappingApi() {

    String apiUrl = "https://www.googleapis.com/geolocation/v1/geolocate?key=xxxxxxxxxxxxxxxxxxxxxxxx";

InputStream inputStream = null;
HttpsURLConnection con = null;
DataOutputStream wr = null;
try {
    URL url = new URL(apiUrl);                  
    con = (HttpsURLConnection) url.openConnection();
        con.setRequestMethod("POST");
    con.setRequestProperty("Content-Type", "application/json");
    con.setDoInput(true);
    con.setDoOutput(true);
    con.connect();
    JSONObject obj = new JSONObject();
    JSONArray wifiAry = new JSONArray();
    JSONObject wifiObj = new JSONObject();
    wifiObj.put("macAddress", "6c:f3:7f:4b:37:74");
    wifiObj.put("signalStrength", 60);
        wifiAry.add(wifiObj);
    wifiObj = new JSONObject();
    wifiObj.put("macAddress", "6c:f3:7f:4b:37:75");
    wifiObj.put("signalStrength", 60);
    wifiAry.add(wifiObj);
    obj.put("wifiAccessPoints", wifiAry);
    System.out.println(obj.toString());
    wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(obj.toString());
    wr.flush();
    wr.close();
    int responseCode = con.getResponseCode();
    inputStream = null;
    if (responseCode == HttpsURLConnection.HTTP_OK) {
    inputStream = con.getInputStream();
        } else {
    inputStream = con.getErrorStream();
        }
    final char[] buffer = new char[4096];
    StringBuilder response = new StringBuilder();
    Reader r = new InputStreamReader(inputStream, "UTF-8");
    int read;
    do {
    read = r.read(buffer, 0, buffer.length);
    if (read > 0) {
        response.append(buffer, 0, read);
    }
    } while (read >= 0);
    System.out.println(new java.util.Date() + " - "
                + response.toString());

} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
    if (inputStream != null)
        inputStream.close();
    if (wr != null)
        wr.close();
    if (con != null)
        con.disconnect();
    } catch (Exception e) {
        }
}

}

输入 JSON 字符串

{"wifiAccessPoints":[{"signalStrength":60,"macAddress":"6c:f3:7f:4b:37:74"}, {"signalStrength":60,"macAddress":"6c:f3:7f: 4b:37:75"}]}

亚马逊 ec2 主机上的响应 { "location": { "lat": 40.603124, "lng": 140.463922 }, "accuracy": 122000.0 }

对我的开发框的响应(Windows 7)

{“位置”:{“纬度”:37.593392,“lng”:-122.04383},“准确度”:22000.0}

4

1 回答 1

0

您可能希望considerIp在 POST 正文中将该字段作为 False 传递。当 wifi 路由器不工作时,这将是 Google 确定的您的位置。

于 2015-08-12T17:32:48.640 回答