日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android获取当前位置的三种方式及其使用方法

發布時間:2024/1/17 Android 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android获取当前位置的三种方式及其使用方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.GPS定位 2.基站定位 此類位置的獲取有賴于手機無線通訊信號,當手機處在信號覆蓋范圍內,手機可以獲得該區域(即通訊術語中的“小區”)的識別號。因為這些識別號是惟一的,因此可以將識別號和地理坐標對應起來,因此根據識別號就可以知道地理位置。但是誤差比較大。 MCC(Mobile Country Code)、MNC(Mobile Network Code)、LAC(Location Aera Code)、CID(Cell Tower ID)是通訊業內的名詞。MCC標識國家,MNC標識網絡,兩者組合起來則唯一標識一家通訊運營商。從維基百科上了解到,一個國家的MCC不唯一,例如中國有460和461,一家運營商也不只一個MNC,例如中國移動有00、02、07。LAC標識區域,類似于行政區域,運營商將大區域劃分成若干小區域,每個區域分配一個LAC。CID標識基站,若手機處在工作狀態,則必須要和一個通訊基站進行通訊,通過CID就可以確定手機所在的地理范圍。 在Android當中,大部分和通訊網絡相關的信息都需要經過一項系統服務,即TelephoneManager來獲得。 TelephonyManager mTelMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); String operator = mTelMan.getNetworkOperator(); String mcc = operator.substring(0, 3); String mnc = operator.substring(3); GsmCellLocation location = (GsmCellLocation) mTelMan.getCellLocation(); int cid = location.getCid(); int lac = location.getLac(); 通過上面的方法,可獲得MCC、MNC、CID、LAC,對照Geolocation API Network Protocol,剩下不多的參數也可以獲得,發起請求后根據響應內容即可得到地理位置信息。 請求(Request)的信息如下: {"cell_towers":[{"mobile_network_code":"00","location_area_code":9733, "mobile_country_code":"460","cell_id":17267},{"mobile_network_code":"00","location_area_code":9733, "mobile_country_code":"460","cell_id":27852},{"mobile_network_code":"00","location_area_code":9733, "mobile_country_code":"460","cell_id":27215},{"mobile_network_code":"00","location_area_code":9733, "mobile_country_code":"460","cell_id":27198},{"mobile_network_code":"00","location_area_code":9484, "mobile_country_code":"460","cell_id":27869},{"mobile_network_code":"00","location_area_code":9508, "mobile_country_code":"460","cell_id":37297},{"mobile_network_code":"00","location_area_code":9733, "mobile_country_code":"460","cell_id":27888}], "host":"maps.google.com", "version":"1.1.0"} 響應(Response)的信息如下: {"location":{"latitude":23.12488,"longitude":113.271907,"accuracy":630.0},"access_token":"2:61tEAW-rONCT1_W-:JVpp2_jq5a0L-5JK"} 3.WIFI定位 其原理是首先收集每個WIFI無線接入點的位置,對每個無線路由器進行唯一的標識,在數據庫中注明這些接入點的具體位置。 使用時,一旦發現有WI-FI接入點,則進入到數據中查看匹配的記錄,進而得到位置信息。 WIFI定位主要取決于節點(node)的物理地址(mac address)。與提供TelephoneManager一樣,Android也提供了獲取WIFI信息的接口:WifiManager。 WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifiMan.getConnectionInfo(); String mac = info.getMacAddress(); String ssid = info.getSSID(); 通過上面的方法,即可獲得必要的請求參數。 發出的請求(Request)信息如下: {"wifi_towers":[{"mac_address":"00:23:76:AC:41:5D","ssid":"Aspire-NETGEAR"}],"host":"maps.google.com","version":"1.1.0"} 響應(Response)的信息如下: {"location":{"latitude":23.129075,"longitude":113.264423,"accuracy":140000.0},"access_token":"2:WRr36ynOz_d9mbw5:pRErDAmJXI8l76MU"} 以上簡單介紹了android中獲取位置的三種定位方式。 實際開發中可利用android.location中的LocationManager類,該類封裝了地理位置信息的接口,提供了GPS_PROVIDER和 NETWORK_PROVIDER。 如果開發的應用需要高精確性,那么可使用GPS_PROVIDER,但這也意味著應用無法在室內使用,待機時間縮短,響應時間稍長等問題; 如果開發的應用需要快速反應,對精度要求不怎么高,并且要盡可能節省電量,那么使用NETWORK_PROVIDER是不錯的選擇。 這里提一下,還有一個 PASSIVE_PROVIDER,在實際應用中較少使用。 1.如下代碼就是設置從Network中獲取位置: LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0, mLocLis); 需要對應的權限:android.permission.ACCESS_COARSE_LOCATION 2.如下代碼就是設置從GPS獲取位置: LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE);mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, mLocLis); 需要對應的權限:android.permission.ACCESS_FINE_LOCATION 如果代碼里使用了兩個 PROVIDER,則只需要一個權限即可:android.permission.ACCESS_FINE_LOCATION。 以下是整個過程的代碼: public class DemoActivity extends Activity {private static final String TAG = "DemoActivity";@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }public void onRequestLocation(View view) { switch (view.getId()){ case R.id.gpsBtn: Log.d(TAG, "GPS button is clicked"); requestGPSLocation(); break; case R.id.telBtn: Log.d(TAG, "CellID button is clicked"); requestTelLocation(); break; case R.id.wifiBtn: Log.d(TAG, "WI-FI button is clicked"); requestWIFILocation(); break; case R.id.netBtn: Log.d(TAG, "Network button is clicked"); requestNetworkLocation(); break; } }private void requestTelLocation() { TelephonyManager mTelMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); // MCC+MNC. Unreliable on CDMA networks String operator = mTelMan.getNetworkOperator(); String mcc = operator.substring(0, 3); String mnc = operator.substring(3);GsmCellLocation location = (GsmCellLocation) mTelMan.getCellLocation(); int cid = location.getCid(); int lac = location.getLac();JSONObject tower = new JSONObject(); try { tower.put("cell_id", cid); tower.put("location_area_code", lac); tower.put("mobile_country_code", mcc); tower.put("mobile_network_code", mnc); } catch (JSONException e) { Log.e(TAG, "call JSONObject's put failed", e); }JSONArray array = new JSONArray(); array.put(tower);List<NeighboringCellInfo> list = mTelMan.getNeighboringCellInfo(); Iterator<NeighboringCellInfo> iter = list.iterator(); NeighboringCellInfo cellInfo; JSONObject tempTower; while (iter.hasNext()) { cellInfo = iter.next(); tempTower = new JSONObject(); try { tempTower.put("cell_id", cellInfo.getCid()); tempTower.put("location_area_code", cellInfo.getLac()); tempTower.put("mobile_country_code", mcc); tempTower.put("mobile_network_code", mnc); } catch (JSONException e) { Log.e(TAG, "call JSONObject's put failed", e); } array.put(tempTower); }JSONObject object = createJSONObject("cell_towers", array); requestLocation(object); }private void requestWIFILocation() { WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifiMan.getConnectionInfo(); String mac = info.getMacAddress(); String ssid = info.getSSID();JSONObject wifi = new JSONObject(); try { wifi.put("mac_address", mac); wifi.put("ssid", ssid); } catch (JSONException e) { e.printStackTrace(); }JSONArray array = new JSONArray(); array.put(wifi);JSONObject object = createJSONObject("wifi_towers", array); requestLocation(object); }private void requestLocation(JSONObject object) { Log.d(TAG, "requestLocation: " + object.toString()); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.google.com/loc/json"); try { StringEntity entity = new StringEntity(object.toString()); post.setEntity(entity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); }try { HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent())); StringBuffer buffer = new StringBuffer(); String result = br.readLine(); while (result != null) { buffer.append(result); result = br.readLine(); }Log.d(TAG, buffer.toString()); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }private JSONObject createJSONObject(String arrayName, JSONArray array) { JSONObject object = new JSONObject(); try { object.put("version", "1.1.0"); object.put("host", "maps.google.com"); object.put(arrayName, array); } catch (JSONException e) { Log.e(TAG, "call JSONObject's put failed", e); } return object; }private void requestGPSLocation() { LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocMan.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 60, 100, mLocLis); }private void requestNetworkLocation() { LocationManager mLocMan = (LocationManager) getSystemService(Context.LOCATION_SERVICE); mLocMan.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000 * 60, 100, mLocLis); }private LocationListener mLocLis = new LocationListener() {@Override public void onStatusChanged(String provider, int status, Bundle extras) { Log.d(TAG, "onStatusChanged, provider = " + provider); }@Override public void onProviderEnabled(String provider) { Log.d(TAG, "onProviderEnabled, provider = " + provider); }@Override public void onProviderDisabled(String provider) { Log.d(TAG, "onProviderDisabled, provider = " + provider); }@Override public void onLocationChanged(Location location) { double latitude = location.getLatitude(); double longitude = location.getLongitude(); Log.d(TAG, "latitude: " + latitude + ", longitude: " + longitude); } }; } 本篇文章來源于網絡,尚未對其進行實踐測試,因為實際應用的關系,這里僅對NetWork方式獲取位置做了實踐,請穩步 Android使用NetWork方式獲取當前的地理位置 一文查看相關內容

總結

以上是生活随笔為你收集整理的Android获取当前位置的三种方式及其使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。