最近做項目時用到了定位功能,百度彻磁、高德都有用到了,這次需求比較簡單一點狸捅,只要定位到城市就可以了衷蜓,所以這次決定使用android原生的定位功能
一、獲取權(quán)限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
定位的實現(xiàn)
package com.example.mystudy.commont;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import androidx.core.app.ActivityCompat;
import com.example.mystudy.MainActivity;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class GPS {
private Context context;
private LocationManager locationManager;
private static final String TAG = "GPS-Info";
public GPS(Context context) {
this.context = context;
initLocationManager();
}
/**
* 獲取權(quán)限尘喝,并檢查有無開戶GPS
*/
private void initLocationManager() {
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
// 轉(zhuǎn)到手機設(shè)置界面磁浇,用戶設(shè)置GPS
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
getProviders();
}
/**
* 獲取可定位的方式
*/
private MyLocationListener myLocationListener;
private String bestProvider;
private void getProviders() {
//獲取定位方式
List<String> providers = locationManager.getProviders(true);
for(String s:providers){
Log.e(TAG,s);
}
Criteria criteria = new Criteria();
// 查詢精度:高,Criteria.ACCURACY_COARSE比較粗略朽褪,Criteria.ACCURACY_FINE則比較精確
criteria.setAccuracy(Criteria.ACCURACY_FINE);
// 是否查詢海撥:否
criteria.setAltitudeRequired(true);
// 是否查詢方位角 : 否
criteria.setBearingRequired(false);
// 設(shè)置是否要求速度
criteria.setSpeedRequired(false);
// 電量要求:低
criteria.setPowerRequirement(Criteria.ACCURACY_LOW);
bestProvider = locationManager.getBestProvider(criteria, false); //獲取最佳定位
myLocationListener = new MyLocationListener();
}
public void startLocation(){
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Log.e(TAG, "startLocation: ");
locationManager.requestLocationUpdates(bestProvider, 0, 0, myLocationListener);
}
public void stopLocation(){
Log.e(TAG, "stopLocation: ");
locationManager.removeUpdates(myLocationListener);
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location location) {
//定位時調(diào)用
Log.e(TAG,"onLocationChanged");
List<Address> addresses = new ArrayList<>();
//經(jīng)緯度轉(zhuǎn)城市
Geocoder geocoder = new Geocoder(context);
try {
addresses =geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),10);
} catch (IOException e) {
e.printStackTrace();
}
for(Address address:addresses){
//國家 CN
Log.e(TAG,address.getCountryCode());
//國家
Log.e(TAG,address.getCountryName());
//省置吓,市无虚,地址
Log.e(TAG,address.getAdminArea());
Log.e(TAG,address.getLocality());
Log.e(TAG,address.getFeatureName());
//經(jīng)緯度
Log.e(TAG, String.valueOf(address.getLatitude()));
Log.e(TAG, String.valueOf(address.getLongitude()));
// Log.e(TAG,address.getAddressLine());
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
//定位狀態(tài)改變
Log.e(TAG,"onStatusChanged");
}
@Override
public void onProviderEnabled(String provider) {
//定位開啟
Log.e(TAG,"onProviderEnabled");
}
@Override
public void onProviderDisabled(String provider) {
//定位關(guān)閉
Log.e(TAG,"onProviderDisabled");
}
}
}
權(quán)限的獲取可參考:Android 動態(tài)權(quán)限申請的封裝
CSDN地址:android原生的定位實現(xiàn)