是否可以将 Google Maps Geolocation API 用于 Android Tv? [英] Is it possible to use Google Maps Geolocation API for Android Tv?

查看:27
本文介绍了是否可以将 Google Maps Geolocation API 用于 Android Tv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was created one application for android TV which is already in Google Play store and works fine.

现在我的新要求是在 Android TV 中使用 Google Maps Geolocation API 所以我被推荐 这个链接 但没有运气.

Now my new requirement is to use the Google Maps Geolocation API in Android TV so I was refereed this link but no luck.

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

So my question is that Is it possible to use Google Maps Geolocation API in Android TV?

推荐答案

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

After many research finally I got the answer.Yes it is possible to use Google Maps Geolocation API in Android TV but with some limitation.As per the documentation given by the The Google Maps Geolocation API their are mainly Two way to achieve this

1) 使用手机信号塔.

1) Using Cell tower.

2) 使用 WiFi 接入点.

2) Using WiFi Access Points.

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

Now, For the 1st way ofCourse it is not possible because Android TV do not have Calling or SIM facility so it can not connect to Cell Tower so it is not working.

现在,第二种方式很有趣.我正在研究这个东西,最后成功实现了这个东西.我注意到的另一件事也在文档中给出,使用 IP 地址它将提供最高的准确率,然后是蜂窝塔和 WIFI 接入点.所以,我考虑了 WIFI 接入点和 "thinkIp": "true" 获得最高准确率.

Now, For the 2nd way it is quite interesting. I was research on this thing and finally Successfully implemented this thing. Another thing I noticed that and also given in the documentation that using IP address it will give the highest accuracy ratio then the Cell Tower and WIFI access points.So, I consider both WIFI access points and "considerIp": "true" for highest accuracy ratio.

经过大量研究后,我的实际任务是实现这件事,我很容易实现这一点,因为我知道如何获取 WIFI 接入点.所以,我使用以下方式获取 WIFI 接入点.

After this much of research my actual task is to implement this thing it is quite easy for me to implement this because I know how to get the WIFI Access Points.So, I used the following way to get WIFI Access Points.

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,这里是我使用的方式.

After getting list of WIFI Access Points create a JSONObject which Pass to Call the API "https://www.googleapis.com/geolocation/v1/geolocate?key=your_key".I was used Volley to call this API.For creating the JSONObject which contains WIFI Access points here is the way which I was used.

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("thinkIp", false)" 使用false背后的原因只是为了检查我是否使用WIFI接入点获得了准确的结果.你可以做到true 并查看结果.

From the above code I was used "parent.put("considerIp", false)" the reason behind use false was nothing but just to checked whether I got the accurate result using WIFI Access Points or not.You can make it true and see the result what you get.

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

After successful response you got the result which gives the Latitude and Longitude with the Accuracy ratio which shows how accurate the result was.You got the response something like this.

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

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

This is the way to achieve Google Maps Geolocation API in Android TV that's it.

这篇关于是否可以将 Google Maps Geolocation API 用于 Android Tv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆