【问题标题】:Detect age of network need for Google Maps Geolocation API检测 Google Maps Geolocation API 的网络需求年龄
【发布时间】:2015-07-28 23:05:27
【问题描述】:

我正在使用 Google Maps Geolocation API 创建一个应用程序,该应用程序将从网络获取用户的位置。 Google Maps Geolocation API 有一些属性可以在 url 中发送以从 Google 获取正确的输出。我的问题是

  1. 信号塔对象 - 年龄 供参考你可以在这里查看Cell tower objects

  2. WiFi 接入点对象 - age , channel , signalToNoiseRatio 供参考,您可以在这里查看WiFi access point object

我尝试了 TelePhonyManager 和 Wifi ScaneResult(),但无法获得这些值

如果有人知道或访问 Google Maps Geolocation API,请提出建议,以便我将这些值传递给 Google Map API 以获得最佳结果

等待回复 谢谢

【问题讨论】:

  • TelePhonyManager 是 Android SDK 的一部分,Cell tower objectsWiFi access point objects 是 Google Maps Javascript 网络服务的一部分。因此,您可以执行网络请求以在您的 Android 应用程序中获取这些对象(例如,asyncTask),并从 API 响应中解析 JSON 结果,然后使用结果更新您的 mapView。样品请求:URL requestUrl = new URL("https://www.googleapis.com/geolocation/v1/geolocate?key=" + &key=" + API_KEY);
  • 抱歉回复晚了,我明白了你的意思,我开始着手处理了网址通过 Json,请提供示例它将帮助我@ztan

标签: android google-maps google-maps-api-3 android-wifi telephonymanager


【解决方案1】:

您可以使用AsyncTask 进行网络请求。

示例代码:

  private class GetGeolocationTask extends AsyncTask<Object, Void, String> {

        @Override
        protected String doInBackground(Object... arg0) {
            InputStream inputStream = null;
            String result = "";

            try {
                // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();

                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost("https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY");

                String json = "";

                // 3. build jsonObject
                JSONObject jsonObject = new JSONObject();
                jsonObject.accumulate("homeMobileCountryCode", 310);
                jsonObject.accumulate("homeMobileNetworkCode", 410);
                jsonObject.accumulate("radioType", "gsm");
                jsonObject.accumulate("carrier", "vodafone");

                JSONArray cellTowersArray = new JSONArray();
                JSONObject cellTowerObject = new JSONObject();
                cellTowerObject.accumulate("cellId", 42);
                cellTowerObject.accumulate("locationAreaCode", 415);
                cellTowerObject.accumulate("mobileCountryCode", 310);
                cellTowerObject.accumulate("mobileNetworkCode", 410);
                cellTowerObject.accumulate("age", 0);
                cellTowerObject.accumulate("signalStrength", -60);
                cellTowerObject.accumulate("timingAdvance", 15);
                cellTowersArray.put(cellTowerObject);
                jsonObject.accumulate("cellTowers", cellTowersArray);

                JSONArray wifiAccessPointsArray = new JSONArray();
                JSONObject wifiAccessPointObject = new JSONObject();
                wifiAccessPointObject.accumulate("macAddress", "01:23:45:67:89:AB");
                wifiAccessPointObject.accumulate("age", 0);
                wifiAccessPointObject.accumulate("channel", 11);
                wifiAccessPointObject.accumulate("signalToNoiseRatio", 40);
                wifiAccessPointsArray.put(wifiAccessPointObject);
                jsonObject.accumulate("wifiAccessPoints", wifiAccessPointsArray);

                // 4. convert JSONObject to JSON to String
                json = jsonObject.toString();


                // 5. set json to StringEntity
                StringEntity se = new StringEntity(json);

                // 6. set httpPost Entity
                httpPost.setEntity(se);

                // 7. Set some headers to inform server about the type of the content
                httpPost.setHeader("Accept", "application/json");
                httpPost.setHeader("Content-type", "application/json");

                // 8. Execute POST request to the given URL
                HttpResponse httpResponse = httpclient.execute(httpPost);

                // 9. receive response as inputStream
                inputStream = httpResponse.getEntity().getContent();

                // 10. convert inputstream to string
                if(inputStream != null)
                    result = convertInputStreamToString(inputStream);
                else
                    result = "Did not work";
            }
            catch (MalformedURLException e) {
                logException(e);
            }
            catch (IOException e) {
                logException(e);
            }
            catch (Exception e) {
                logException(e);
            }

            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            Toast.makeText(YourActivity.this, result, Toast.LENGTH_LONG).show();
        }
    }

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{
          BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
          String line = "";
          String result = "";
          while((line = bufferedReader.readLine()) != null)
              result += line;

          inputStream.close();
          return result;
    }   

您可以像这样在活动中调用此 AsyncTask:

GetGeolocationTask getBlogPostsTask = new GetGeolocationTask();
getGeolocationTask.execute(); 

您还可以阅读this tutorial,了解有关如何将 JSON 放入请求正文的详细信息。

或者,你可以使用Google Volley库来做你的网络请求,示例请求可以在this Stackoverflow answer找到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-22
    • 2013-07-15
    • 2017-12-29
    • 2020-08-11
    • 1970-01-01
    • 2014-11-05
    • 2014-10-12
    • 1970-01-01
    相关资源
    最近更新 更多