使用经纬度得到位置Geocorder
先得到经纬度再用geocorder 显示位置,需要手机打开位置权限,使用GPS的话把注释去掉,GPS在室内很容易收不到信号,得到位置为空
public class MainActivity extends AppCompatActivity { private LocationManager lm; double latitude; double longitude; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); // if (!lm.isProviderEnabled(LocationManager.GPS_PROVIDER) ? true : false) { // Toast.makeText(MainActivity.this, "请打开GPS~", Toast.LENGTH_SHORT).show(); // Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); // startActivityForResult(intent, 0); // } if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { //位置权限打开检查 return; } Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); // Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); System.out.println("经度"+longitude); } //设置间隔两秒获得一次GPS定位信息 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() { @Override public void onLocationChanged(Location location) { // 当GPS定位信息发生改变时,更新定位 } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @SuppressLint("MissingPermission") @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } }); Geocoder gc = new Geocoder(this, Locale.getDefault()); List<Address> address = null; try { address = gc.getFromLocation(latitude, longitude, 1); } catch(IOException e){ e.printStackTrace(); } Address address2 = address.get(0);//得到Address实例 //Log.i(TAG, "address =" + address); String countryName = address2.getCountryName();//得到国家名称,比方:中国 System.out.println("countryName = "+countryName); String locality = address2.getLocality();//得到城市名称,比方:北京市 System.out.println("locality = "+locality); for(int i = 0; address2.getAddressLine(i)!=null;i++) { String addressLine = address2.getAddressLine(i);//得到周边信息。包含街道等。i=0,得到街道名称 System.out.println("addressLine = " + addressLine); } } }
xmlns:tools="http://schemas.android.com/tools" package="com.january.spring"> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
如果8.0以下geocoder可能无法得到,要把它放在线程执行里才行