Is it possible to use Google Maps Geolocation API

2019-04-16 03:23发布

问题:

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

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

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

回答1:

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) Using Cell tower.

2) Using WiFi Access Points.

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.

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.

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();

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();
    }

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.]
}

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



回答2:

From the 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.

If you follow the link, you'll see some code how the achieve this. Unfortunately you won't get the exact position, but depending on your requirements, this might be sufficient.