android - 是否可以使用适用于 Android TV 的 Google Maps Geolocation API?

标签 android android-tv google-geolocation

我为 Android TV 创建了一个应用程序,它已经在 Google Play 商店中并且运行良好。

现在我的新要求是在 Android TV 中使用 Google Maps Geolocation API 所以我被推荐了 this link但没有运气。

所以我的问题是,是否可以在 Android TV 中使用 Google Maps Geolocation API?

最佳答案

经过多次研究,我终于得到了答案。是的,可以在 Android TV 中使用 Google Maps Geolocation API,但有一些限制。根据 documentation The Google Maps Geolocation API 给出了它们主要有两种实现方式

1) 使用手机信号塔。

2) 使用 WiFi 接入点。

现在,对于第一种方式,当然是不可能的,因为 Android TV 没有通话或 SIM 功能,因此它无法连接到 Cell Tower,因此无法正常工作。

现在,对于第二种方式,它非常有趣。我正在研究这个东西,最后成功地实现了这个东西。我注意到的另一件事,并且在文档中也给出了使用 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 接入点获得了准确的结果。你可以做到true 并查看结果。

成功响应后,您会得到包含纬度和经度的结果以及显示结果准确度的准确率比率。您会得到类似这样的响应。

{
  "location":
  {
     "lat": your latitude,
     "lng": your longitude
  },
 "accuracy": How accurate the result was [like 1523, 1400.25 etc.]
}

这就是在 Android TV 中实现 Google Maps Geolocation API 的方法。

关于android - 是否可以使用适用于 Android TV 的 Google Maps Geolocation API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43893246/

相关文章:

android - 仅在 Android 应用程序的电话簿中同步新插入的联系人

android - 如何制作一个可以在手机和android-TV中使用的apk?

android - 收听 Intent 变化

php - 谷歌地图地理定位不请求许可

javascript - Google GeolocationAPI 未找到请求的实体

google-api - Google 地理编码 API 安全性

java - 在数组中搜索值并获取数组名称

android - CircleProgress 与自定义 StrokeCap Image Flutter

java - openCV 中 BROWN 颜色的 HSV 值范围是多少?

android - MXQ-4K安卓盒子如何连接Linux上的Android Studio