Android定位(经纬度+当前位置信息)
我相信大家在Android开发中应该都有遇到过需要获取经纬度和当前位置信息的情况!
本人目前也还处于学习Android阶段,想做一下关于Android定位的笔记。
以下是本人通过一个下午的时间找到最新可以获取定位信息的方法。希望对大家也有帮助。
申请权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
LocationManage位置管理器
通过LocatioManage去获取到当前的位置管理器
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
使用locationManager.requestLocationUpdates()方法注意
在方法中使用方法头部带上@SuppressLint("MissingPermission")
或者放在判断是否有使用定位的权限语句的后面
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
}
locationManager.requestLocationUpdates(
选中定位模式,
多久获取一次(毫秒为单位),
移动距离多远获取一次(米为单位),
传一个LocationListener监听
);
locationManager.removeUpdates(传入实现过LocationListener的类);
完整代码
MainActivity类的代码
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements LocationListener {
private TextView nowAddress;
private TextView lat;
private TextView lon;
private Double longitude,latitude;
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nowAddress = findViewById(R.id.tv_nowAddress);
lat = findViewById(R.id.tv_latitude);
lon = findViewById(R.id.tv_longitude);
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 100);
}
getLocation();
}
@SuppressLint("MissingPermission")
private void getLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, MainActivity.this);
}
@Override
public void onLocationChanged(@NonNull Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
lat.setText("纬度:" + latitude);
lon.setText("经度:" + longitude);
Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
Address address = addresses.get(0);
String info = address.getAddressLine(0) +
address.getAddressLine(1) +
address.getAddressLine(2);
nowAddress.setText(info);
} catch (IOException e) {
e.printStackTrace();
}
locationManager.removeUpdates(this);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("onStatusChanged", provider);
}
@Override
public void onProviderEnabled(@NonNull String provider) {
Log.e("onProviderEnabled", provider);
}
@Override
public void onProviderDisabled(@NonNull String provider) {
Log.e("onProviderDisabled", provider);
}
}
Layout布局的代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="纬度"
app:layout_constraintBottom_toTopOf="@+id/tv_longitude"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tv_longitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="经度"
app:layout_constraintBottom_toTopOf="@+id/tv_nowAddress"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/tv_latitude" />
<TextView
android:id="@+id/tv_nowAddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="当前位置"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
|