经过多次研究,我终于得到了答案。是的,可以在 Android TV 中使用 Google Maps Geolocation API,但有一些限制。根据 Google Maps Geolocation 给出的documentation API他们主要有两种方式来实现这个
1) 使用手机信号塔。
2) 使用 WiFi 接入点。
现在,对于第一种方式,这是不可能的,因为 Android TV 没有通话或 SIM 功能,因此无法连接到手机信号塔,因此无法正常工作。
现在,对于第二种方式,它非常有趣。我一直在研究这个东西,最后成功实现了这个东西。我注意到的另一件事,并且在文档中也给出了使用 IP 地址将提供比蜂窝塔和 WIFI 接入点更高的准确率。所以,我同时考虑 WIFI 接入点和 "considerIp": "true" 以获得最高的准确率。
经过这么多研究,我的实际任务是实现这个东西,我很容易实现这个,因为我知道如何获取 WIFI 接入点。所以,我使用以下方法来获取 WIFI 接入点。
List<ScanResult> results;
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
results = wifiManager.getScanResults();
String message = "No results found.Please Check your wireless is on";
if (results != null)
{
final int size = results.size();
if (size == 0)
message = "No access points in range";
else
{
ScanResult bestSignal = results.get(0);
int count = 1;
for (ScanResult result : results)
{
if (WifiManager.compareSignalLevel(bestSignal.level, result.level) < 0)
{
bestSignal = result;
}
}
}
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
在获取 WIFI 接入点列表后创建一个 JSONObject 传递调用 API "https://www.googleapis.com/geolocation/v1/geolocate?key=your_key"。我使用 Volley 调用此 API。用于创建包含 WIFI 接入点的 JSONObject这是我使用的方式。
JSONObject parent;
try {
parent = new JSONObject();
parent.put("considerIp", false);
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject;
for (int i = 0; i < results.size(); i++) {
jsonObject = new JSONObject();
jsonObject.put("macAddress", results.get(i).BSSID);
jsonObject.put("signalStrength", results.get(i).level);
jsonObject.put("signalToNoiseRatio", 0);
jsonArray.put(jsonObject);
System.out.println("jsonObject: " + jsonObject.toString(4));
}
parent.put("wifiAccessPoints", jsonArray);
Log.d("output", parent.toString());
System.out.println("parenttttt: " + parent.toString(4));
txt_request.setText(parent.toString(4));
} catch (JSONException e) {
e.printStackTrace();
}
从上面的代码中我使用了“parent.put("considerIp", false)" 使用false的原因只是为了检查我是否使用WIFI接入点得到了准确的结果。你可以做到为真,看看你得到的结果。
成功响应后,您得到的结果给出了纬度和经度以及准确度比率,显示了结果的准确程度。您得到了类似这样的响应。
{
"location":
{
"lat": your latitude,
"lng": your longitude
},
"accuracy": How accurate the result was [like 1523, 1400.25 etc.]
}
这是在Android TV中实现Google Maps Geolocation API的方法。