2017-05-10 35 views

回答

1

經過多年的研究終於有了我的answer.Yes,可以使用谷歌地圖地理位置API在Android TV,但每經的谷歌地圖地理位置API賦予其主要有兩種方式documentation一些limitation.As實現這一點

1)使用單元塔。

2)使用WiFi接入點。

現在,對於第一種課程而言,這是不可能的,因爲Android TV沒有呼叫或SIM卡設施,因此無法連接到Cell Tower,因此無法使用。

現在,對於第二種方式,這是非常有趣的。我正在研究這件事,並最終成功實施了這件事。另一件事我注意到了,並且在使用IP地址的文檔中給出,它將給出最高精度比,然後是Cell Tower和WIFI接入點。因此,我認爲WIFI接入點和「考慮IP」:「真實」最高的準確率。

經過這麼多的研究,我的實際任務是實現這個東西,這對我來說很容易實現,因爲我知道如何獲得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這通通話https://www.googleapis.com/geolocation/v1/geolocate?key=your_key我當時用凌空調用這個API。有關創建包含WIFI接入點,這裏是中的JSONObject的API我曾經使用過的方式。

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)」上面的代碼背後利用虛假的原因是什麼,但只是爲了檢查自己是否使用了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的方式。

0

TV developer guide

電視是固定的,室內設備,不具有內置的全球定位系統(GPS)接收器。如果您的應用使用位置信息,您仍然可以允許用戶搜索位置,或者使用靜態位置提供商,例如在電視設備設置期間配置的郵政編碼。

如果你關注鏈接,你會看到一些代碼如何實現這一點。不幸的是,你不會得到確切的位置,但取決於你的要求,這可能就足夠了。

+0

謝謝你的回答。但是這不會幫助我發佈我已經知道的東西。我的問題是可以在Android電視中使用Google地圖地理定位API。經過一番研究,我得到了答案,但仍然沒有發佈在stackoverflow上。 –

+0

然後請回答你自己的問題,並在第二天將其標記爲解決方案。 –