我们使用Google的地理位置API来获取wifi接入点的latlong。我们看到不一致的结果。 Google的API在不同的框上返回不同的latlong。当我在我的开发箱上测试时,我得到了指向美国某个位置的latlong。当我在亚马逊ec2盒子上测试相同的时候,我得到了指向日本某个位置的latlong。其他人对Google的GeoLocation API也有相同的体验吗?
以下是来自两台主机的代码和响应字符串。两台主机上的JDK和JSON jar版本相同。Google GeoLocation API为同一个WiFi接入点返回不同的结果
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" }]}在Amazon EC2主机上
响应 { “位置”: { “LAT”:40.603124, “LNG”:140.463922 }, “准确度”:在我的开发122000.0 }
响应(Windows 7)中
{ “位置”:{ “LAT”:37.593392, “LNG”:-122.04383 }, “准确度”:22000.0 }
我认为你应该添加你用来查询Google的地理定位API的代码 – elbuild
我已经添加了代码,输入json字符串和来自两个主机的响应字符串。请检查。 – user3208468