這篇文章主要講解了Android如何通過原生方式獲取經(jīng)緯度與城市信息,內(nèi)容清晰明了,對此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會有幫助。
創(chuàng)新互聯(lián)建站成立以來不斷整合自身及行業(yè)資源、不斷突破觀念以使企業(yè)策略得到完善和成熟,建立了一套“以技術(shù)為基點,以客戶需求中心、市場為導(dǎo)向”的快速反應(yīng)體系。對公司的主營項目,如中高端企業(yè)網(wǎng)站企劃 / 設(shè)計、行業(yè) / 企業(yè)門戶設(shè)計推廣、行業(yè)門戶平臺運營、重慶APP開發(fā)公司、手機網(wǎng)站開發(fā)、微信網(wǎng)站制作、軟件開發(fā)、成都聯(lián)通服務(wù)器托管等實行標(biāo)準(zhǔn)化操作,讓客戶可以直觀的預(yù)知到從創(chuàng)新互聯(lián)建站可以獲得的服務(wù)效果。一、概述
在項目中需要獲取用戶所在位置的經(jīng)緯度和城市上送給風(fēng)控系統(tǒng)。一般來說,定位有兩種方式:
本文講述定位的第二種方式--用Android原生的SDK中的api定位,如果項目定位要求較高還是建議使用第三方地圖庫。
二、Android原生SDK中的api定位
Android原生方式獲取經(jīng)緯度兩種定位方式:GPS定位和Wifi定位
示例代碼如下:
public class TestLocationActivity extends AppCompatActivity { public static final int LOCATION_CODE = 301; private LocationManager locationManager; private String locationProvider = null; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); getLocation(); } private void getLocation(){ //1.獲取位置管理器 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //2.獲取位置提供器,GPS或是NetWork List<String> providers = locationManager.getProviders(true); if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; Log.v("TAG", "定位方式GPS"); } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; Log.v("TAG", "定位方式Network"); }else { Toast.makeText(this, "沒有可用的位置提供器", Toast.LENGTH_SHORT).show(); return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //獲取權(quán)限(如果沒有開啟權(quán)限,會彈出對話框,詢問是否開啟權(quán)限) if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //請求權(quán)限 ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, LOCATION_CODE); } else { //3.獲取上次的位置,一般第一次運行,此值為null Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "",Toast.LENGTH_SHORT).show(); Log.v("TAG", "獲取上次的位置-經(jīng)緯度:"+location.getLongitude()+" "+location.getLatitude()); getAddress(location); }else{ //監(jiān)視地理位置變化,第二個和第三個參數(shù)分別為更新的最短時間minTime和最短距離minDistace locationManager.requestLocationUpdates(locationProvider, 3000, 1,locationListener); } } } else { Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); Log.v("TAG", "獲取上次的位置-經(jīng)緯度:"+location.getLongitude()+" "+location.getLatitude()); getAddress(location); }else{ //監(jiān)視地理位置變化,第二個和第三個參數(shù)分別為更新的最短時間minTime和最短距離minDistace locationManager.requestLocationUpdates(locationProvider, 3000, 1,locationListener); } } } public LocationListener locationListener = new LocationListener() { // Provider的狀態(tài)在可用、暫時不可用和無服務(wù)三個狀態(tài)直接切換時觸發(fā)此函數(shù) @Override public void onStatusChanged(String provider, int status, Bundle extras) { } // Provider被enable時觸發(fā)此函數(shù),比如GPS被打開 @Override public void onProviderEnabled(String provider) { } // Provider被disable時觸發(fā)此函數(shù),比如GPS被關(guān)閉 @Override public void onProviderDisabled(String provider) { } //當(dāng)坐標(biāo)改變時觸發(fā)此函數(shù),如果Provider傳進(jìn)相同的坐標(biāo),它就不會被觸發(fā) @Override public void onLocationChanged(Location location) { if (location != null) { //如果位置發(fā)生變化,重新顯示地理位置經(jīng)緯度 Toast.makeText(TestLocationActivity.this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); Log.v("TAG", "監(jiān)視地理位置變化-經(jīng)緯度:"+location.getLongitude()+" "+location.getLatitude()); } } }; @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case LOCATION_CODE: if(grantResults.length > 0 && grantResults[0] == getPackageManager().PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "申請權(quán)限", Toast.LENGTH_LONG).show(); try { List<String> providers = locationManager.getProviders(true); if (providers.contains(LocationManager.NETWORK_PROVIDER)) { //如果是Network locationProvider = LocationManager.NETWORK_PROVIDER; }else if (providers.contains(LocationManager.GPS_PROVIDER)) { //如果是GPS locationProvider = LocationManager.GPS_PROVIDER; } Location location = locationManager.getLastKnownLocation(locationProvider); if (location!=null){ Toast.makeText(this, location.getLongitude() + " " + location.getLatitude() + "", Toast.LENGTH_SHORT).show(); Log.v("TAG", "獲取上次的位置-經(jīng)緯度:"+location.getLongitude()+" "+location.getLatitude()); }else{ // 監(jiān)視地理位置變化,第二個和第三個參數(shù)分別為更新的最短時間minTime和最短距離minDistace locationManager.requestLocationUpdates(locationProvider, 0, 0,locationListener); } }catch (SecurityException e){ e.printStackTrace(); } } else { Toast.makeText(this, "缺少權(quán)限", Toast.LENGTH_LONG).show(); finish(); } break; } } //獲取地址信息:城市、街道等信息 private List<Address> getAddress(Location location) { List<Address> result = null; try { if (location != null) { Geocoder gc = new Geocoder(this, Locale.getDefault()); result = gc.getFromLocation(location.getLatitude(), location.getLongitude(), 1); Toast.makeText(this, "獲取地址信息:"+result.toString(), Toast.LENGTH_LONG).show(); Log.v("TAG", "獲取地址信息:"+result.toString()); } } catch (Exception e) { e.printStackTrace(); } return result; } @Override protected void onDestroy() { super.onDestroy(); locationManager.removeUpdates(locationListener); } }
當(dāng)前名稱:Android如何通過原生方式獲取經(jīng)緯度與城市信息-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://jinyejixie.com/article12/ggsgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、手機網(wǎng)站建設(shè)、全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計、網(wǎng)站策劃、App設(shè)計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)