使用经纬度得到位置Geocorder
阅读原文时间:2023年07月10日阅读:1

先得到经纬度再用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">





<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>

如果8.0以下geocoder可能无法得到,要把它放在线程执行里才行

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章