Google 地图地理定位 API 是否可以用于 Android 电视?

Is it possible to use Google Maps Geolocation API for Android Tv?

我为 android 电视创建了一个应用程序,该应用程序已经在 Google Play 商店中并且运行良好。

现在我的新要求是在 Android 电视中使用 Google 地图地理定位 API 所以我被推荐了 this link 但没有运气。

所以我的问题是,是否可以在 Android 电视中使用 Google 地图地理定位 API?

来自TV developer guide

TVs are stationary, indoor devices, and do not have built-in global positioning system (GPS) receivers. If your app uses location information, you can still allow users to search for a location, or use a static location provider such as a zip code configured during the TV device setup.

如果您遵循 link,您将看到一些代码是如何实现这一点的。很遗憾,您无法获得确切位置,但根据您的要求,这可能就足够了。

经过多次研究,我终于得到 answer.Yes 可以在 Android 中使用 Google 地图地理定位 API电视,但根据 Google 地图地理定位 API 给出的 documentation 有一些 limitation.As 它们主要是实现此目的的两种方法

1) 使用手机信号塔。

2) 使用 WiFi 接入点。

现在,对于课程的第一种方式,它是不可能的,因为 Android 电视没有通话或 SIM 功能,因此它无法连接到手机信号塔,因此无法正常工作。

现在,对于第二种方式,它很有趣。我正在研究这个东西,最后成功地实现了这个东西。另一件事我注意到,并且在文档中也给出了使用 IP 地址它将提供最高的准确率,然后是 Cell Tower 和 WIFI 访问 points.So,我考虑了 WIFI 访问点和 "considerIp": "true" 最高准确率。

经过这么多的研究,我的实际任务是实现这个东西,我很容易实现这个,因为我知道如何获得 WIFI 访问 Points.So,我使用以下方式获得 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.For 创建包含 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 接入点或not.You 可以让它成真,看看你得到的结果。

成功响应后,您得到的结果给出了纬度和经度以及准确度比率,显示了结果的准确度 was.You 得到了类似这样的响应。

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

这是在 Android 电视中实现 Google 地图地理定位 API 的方法。