我正在尝试使用 Google 地图地理定位 API。我正在将仅包含手机信号塔的 JSON 文件发布到https://www.googleapis.com/geolocation/v1/geolocate?key=MYKEY.
JSON 示例:
{
"cellTowers":[
{
"cellId":65535,
"locationAreaCode":5160,
"mobileCountryCode":234,
"mobileNetworkCode":10,
"signalStrength":-53
},
{
"cellId":34177,
"locationAreaCode":5160,
"mobileCountryCode":234,
"mobileNetworkCode":10,
"signalStrength":-55
}
]}
Run Code Online (Sandbox Code Playgroud)
这已被验证为正确的 JSON。然而,我在从 API 获取合适的数据时遇到了问题。
我已经使用 Apache HTTPClient 和 HTTPPost 库在 Java 中实现了 POST。我收到了有效的响应,但它始终是 cellTowers 阵列中第一个蜂窝塔的 LAT、LNG。就好像 API 正在读取第一个手机信号塔,获取位置然后忽略其余部分。当然,我已经通过反转和洗牌列表来验证这是行为,并且每次到第一个塔位置时响应都会发生变化。这是我的代码:
HttpParams httpParams = new BasicHttpParams();
ConnRouteParams.setDefaultProxy(httpParams, new HttpHost(PROXY_HOST, PROXY_PORT));
HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
HttpClient client = new DefaultHttpClient(httpParams);
HttpPost request = new HttpPost(GOOGLE_GEOLOCATION_API);
request.setHeader("Content-type", "application/json");
try {
StringEntity se = new StringEntity(json);
request.setEntity(se);
HttpResponse response = client.execute(request);
if(response!=null){
String jsonResult = EntityUtils.toString(response.getEntity());
GeolocationResponse geolocationResponse = gson.fromJson(jsonResult, GeolocationResponse.class);
logger.info(jsonResult);
logger.info("Est. Location: " + geolocationResponse.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
地理定位 API 一次仅支持一个手机信号塔吗?我认为它会比这更聪明,使用某种信号传播和跨所有塔的距离估计。
| 归档时间: |
|
| 查看次数: |
2133 次 |
| 最近记录: |