是否可以将Android Maps Geolocation API用于Android电视?

时间:2017-05-10 12:59:40

标签: android android-tv google-geolocation

我创建了一个Android TV应用程序,已经在Google Play商店中运行正常。

现在我的新要求是在Android TV中使用 Google Maps Geolocation API ,因此我被评为https://jsfiddle.net/w6br9r69/2/,但没有运气。

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

2 个答案:

答案 0 :(得分:2)

经过多次研究后,我得到了答案。是否可以在Android TV中使用 Google Maps Geolocation API ,但有一些限制。根据Google提供的documentation Maps Geolocation API主要是实现此目的的两种方法

1)使用Cell tower。

2)使用WiFi接入点。

现在,对于第一种方式的课程,这是不可能的,因为Android TV没有呼叫或SIM设施,因此它无法连接到Cell Tower,因此无法正常工作。

现在,对于第二种方式,它非常有趣。我正在研究这个问题,最后成功实现了这个东西。我注意到的另一件事,也在文档中给出了使用IP地址,它将提供最高的准确率,然后是Cell Tower和WIFI接入点。所以,我考虑了WIFI接入点和“ConsideIp”:“true” ,以获得最高的准确率。

经过这么多的研究,我的实际任务是实现这个,我很容易实现这个,因为我知道如何获得WIFI接入点。所以,我使用以下方式获取WIFI接入点。< / p>

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访问点列表后,创建一个调用API的JSONObject 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(”ConsideIp“,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的方法。

答案 1 :(得分:0)

来自TV developer guide

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

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