要實(shí)現(xiàn)Android中的GPS定位功能,你可以按照以下步驟進(jìn)行操作:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// 如果沒(méi)有權(quán)限,可以向用戶請(qǐng)求權(quán)限
return;
}
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// 當(dāng)位置更新時(shí)調(diào)用
double latitude = location.getLatitude();
double longitude = location.getLongitude();
// 可以在這里對(duì)位置進(jìn)行處理
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.removeUpdates(locationListener);
這樣,你就可以在Android中實(shí)現(xiàn)GPS定位功能了。