Google GeoLocation API会为同一个wifi接入点返回不同的结果

时间:2014-01-17 23:37:50

标签: google-geolocation

我们正在使用Google的地理位置API来获取wifi接入点的latlong。我们看到的结果不一致。谷歌的API在不同的盒子上返回不同的latlong。当我在我的开发盒上测试时,我得到的latlong指向美国的一个位置。当我在亚马逊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主机的回应 {     “地点”: {         “lat”:40.603124,         “lng”:140.463922      },      “准确度”:122000.0 }

我的开发框响应(Windows 7)

{     “地点”: {         “lat”:37.593392,         “lng”: - 122.04383     },     “准确度”:22000.0 }

1 个答案:

答案 0 :(得分:0)

您可能希望在POST正文中将considerIp字段传递为False。当无线路由器无法正常工作时,这将是谷歌确定的位置。