【问题标题】:Is it possible to use Google Maps Geolocation API for Android Tv?是否可以在 Android 电视上使用 Google Maps Geolocation API?
【发布时间】:2017-10-09 03:32:42
【问题描述】:

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

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

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

【问题讨论】:

    标签: android android-tv google-geolocation


    【解决方案1】:

    经过多次研究,我终于得到了答案。是的,可以在 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的方法。

    【讨论】:

      【解决方案2】:

      来自TV developer guide

      电视是固定的室内设备,没有内置的全球定位系统 (GPS) 接收器。如果您的应用使用位置信息,您仍然可以允许用户搜索位置,或使用静态位置提供程序,例如在电视设备设置期间配置的邮政编码。

      如果您点击链接,您将看到一些代码如何实现这一点。不幸的是,您不会得到确切的位置,但根据您的要求,这可能就足够了。

      【讨论】:

      • 感谢您的回答。但这不会帮助我发布我已经知道的内容。我的问题是是否可以在 android 电视中使用 Google Maps Geolocation API。经过一番研究,我得到了答案,但仍未在 stackoverflow 上发布。
      • 那么请回答您自己的问题,并在第二天将其标记为解决方案。
      猜你喜欢
      • 1970-01-01
      • 2013-07-15
      • 2020-08-11
      • 2014-10-12
      • 2014-11-05
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多