实际上我从这段代码中得不到经纬度。
tap
//这里是活动权限 if(ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_COARSE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
package com.example.reyadmahabub.location;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
public class MainActivity extends AppCompatActivity {
private LocationCallback callback;
private LocationRequest request;
private FusedLocationProviderClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
client = LocationServices.getFusedLocationProviderClient( this );
callback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult( locationResult );
for (Location location:locationResult.getLocations()){
double lat=location.getLatitude();
double lon=location.getLongitude();
Toast.makeText( MainActivity.this,lat+","+lon,
Toast.LENGTH_SHORT ).show();
}
}
};
createLocationRequest();
}
private void createLocationRequest() {
request = new
LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY )
.setInterval( 2500 ).setFastestInterval( 5000 );
答案 0 :(得分:0)
更新您的代码:
LocationRequest()。setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY ).setInterval(0).setFastestInterval(0);
请手动向您的应用程序授予位置权限(因为您的代码会忽略此权限)
答案 1 :(得分:0)
使用此代码获取经度和纬度:
public static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
private boolean mLocationPermissionsGranted = false;
private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
private boolean mLocationPermissionsGranted = false;
private FusedLocationProviderClient mFusedLocationProviderClient;
private Location deviceLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getLocationPermission();
if(mLocationPermissionsGranted){
getDeviceLocation();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
Log.d(TAG, "onRequestPermissionsResult: called.");
mLocationPermissionsGranted = false;
switch(requestCode){
case LOCATION_PERMISSION_REQUEST_CODE:{
if(grantResults.length > 0){
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
mLocationPermissionsGranted = false;
Log.d(TAG, "onRequestPermissionsResult: permission failed");
return;
}
}
Log.d(TAG, "onRequestPermissionsResult: permission granted");
mLocationPermissionsGranted = true;
//initialize
getDeviceLocation();
}
}
}
}
private void getLocationPermission(){
Log.d(TAG, "getLocationPermission: getting location permissions");
String[] permissions = {FINE_LOCATION};
if(ContextCompat.checkSelfPermission(getApplicationContext(),
FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
mLocationPermissionsGranted = true;
}else{
ActivityCompat.requestPermissions(CompassActivity.this,
permissions,
LOCATION_PERMISSION_REQUEST_CODE);
}
}
private void getDeviceLocation(){
Log.d(TAG, "getDeviceLocation: getting the devices current location");
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
try{
if(mLocationPermissionsGranted){
final Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Log.d(TAG, "onComplete: found location!");
deviceLocation = (Location) task.getResult();
//Your code here
}
}
});
}else
Log.e(TAG, "getDeviceLocation: Permission Failed");
}catch (SecurityException e){
Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage() );
}
}