android如何获取经纬度
android定位的两种方式:GPS_PROVIDERandNETWORK_PROVIDER
定位的可以借助LocationManager来实现
MainActivity代码
staticfinalStringTAG="MainActivity"; privateTextViewlocationTV; privateLocationManagerlocationManager; privateStringprovider; ArrayListdataList=newArrayList (); @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //initView(); locationTV=(TextView)findViewById(R.id.locaiton_tv); locationManager=(LocationManager)getSystemService(this.LOCATION_SERVICE); //获取所有可用的位置提供器 List providerList=locationManager.getProviders(true); if(providerList.contains(LocationManager.GPS_PROVIDER)){ provider=LocationManager.GPS_PROVIDER; }elseif(providerList.contains(LocationManager.NETWORK_PROVIDER)){ provider=LocationManager.NETWORK_PROVIDER; }else{ //当没有可用的位置提供器时,弹出Toast提示用户 Toast.makeText(this,"Nolocationprovidertouse",Toast.LENGTH_SHORT).show(); return; } Locationlocation=locationManager.getLastKnownLocation(provider); if(location!=null){ //显示当前设备的位置信息 showLocation(location); } if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ //TODO:Considercalling //ActivityCompat#requestPermissions //heretorequestthemissingpermissions,andthenoverriding //publicvoidonRequestPermissionsResult(intrequestCode,String[]permissions, //int[]grantResults) //tohandlethecasewheretheusergrantsthepermission.Seethedocumentation //forActivityCompat#requestPermissionsformoredetails. return; } locationManager.requestLocationUpdates(provider,5000,1,locationListener); } privatevoidshowLocation(Locationlocation){ StringcurrentPosition="latitudeis"+location.getLatitude()+"\n"+"longitudeis"+location.getLongitude(); locationTV.setText(currentPosition); } @Override protectedvoidonDestroy(){ super.onDestroy(); if(locationManager!=null){ if(ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED&&ActivityCompat.checkSelfPermission(this,android.Manifest.permission.ACCESS_COARSE_LOCATION)!=PackageManager.PERMISSION_GRANTED){ //TODO:Considercalling //ActivityCompat#requestPermissions //heretorequestthemissingpermissions,andthenoverriding //publicvoidonRequestPermissionsResult(intrequestCode,String[]permissions, //int[]grantResults) //tohandlethecasewheretheusergrantsthepermission.Seethedocumentation //forActivityCompat#requestPermissionsformoredetails. return; } //关闭程序时将监听器移除 locationManager.removeUpdates(locationListener); } } LocationListenerlocationListener=newLocationListener(){ @Override publicvoidonLocationChanged(Locationlocation){ Toast.makeText(MainActivity.this,"onLocationChanged",Toast.LENGTH_SHORT).show(); } @Override publicvoidonStatusChanged(Strings,inti,Bundlebundle){ Toast.makeText(MainActivity.this,"onStatusChanged",Toast.LENGTH_SHORT).show(); } @Override publicvoidonProviderEnabled(Strings){ Toast.makeText(MainActivity.this,"onProviderEnabled",Toast.LENGTH_SHORT).show(); } @Override publicvoidonProviderDisabled(Strings){ Toast.makeText(MainActivity.this,"onProviderDisabled",Toast.LENGTH_SHORT).show(); } };
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。