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



我们正在使用谷歌的地理定位API来获取wifi接入点的latlong。我们看到了不一致的结果。谷歌的API在不同的框上返回不同的latlong。当我在我的开发框上测试时,我会得到指向美国某个位置的latlong。当我在亚马逊ec2框上测试同样的东西时,我得到指向日本某个位置。有没有其他人对谷歌的地理定位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}

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

最新更新