Android FusedLocationProviderClient未返回位置

Android FusedLocationProviderClient未返回位置,android,geolocation,android-fusedlocation,Android,Geolocation,Android Fusedlocation,我正在使用FusedLocationProviderClient通过单击按钮获取纬度和经度。但当我点击按钮时,它什么也没显示。只是应用程序一直在加载。有人能告诉我我在代码中哪里做错了吗 代码如下: public class MainActivity extends AppCompatActivity { Button showLocation; TextView getLat, getLong; @Override protected void onCreat

我正在使用
FusedLocationProviderClient
通过单击按钮获取纬度和经度。但当我点击按钮时,它什么也没显示。只是应用程序一直在加载。有人能告诉我我在代码中哪里做错了吗

代码如下:

public class MainActivity extends AppCompatActivity {

    Button showLocation;
    TextView getLat, getLong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        showLocation = findViewById(R.id.btnShowLocation);
        getLat = findViewById(R.id.getLat);
        getLong = findViewById(R.id.getLong);
    }

    public void getlocation(View v) {
        final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Getting Location");
        progressDialog.setCancelable(false);
        progressDialog.show();
        FusedLocationProviderClient fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);
        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, 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;
        }
        Task<Location> task = fusedLocationProviderClient.getLastLocation();
        task.addOnSuccessListener(new OnSuccessListener<Location>() {
            @Override
            public void onSuccess(Location location) {
                if(location!=null)
                {
                    String lati=String.valueOf(location.getLatitude());
                    getLat.setText(lati);

                    String longi=String.valueOf(location.getLongitude());
                    getLat.setText(longi);

                    Toast.makeText(MainActivity.this,"Location Found !!!",Toast.LENGTH_LONG).show();
                    progressDialog.cancel();
                }
                else
                    Toast.makeText(MainActivity.this,"Please Enable GPS And Internet !!!",Toast.LENGTH_LONG).show();
            }
        });
    }
}
public类MainActivity扩展了AppCompatActivity{
按钮显示位置;
text查看getLat,getLong;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showLocation=findViewById(R.id.btnShowLocation);
getLat=findviewbyd(R.id.getLat);
getLong=findviewbyd(R.id.getLong);
}
公共位置(视图v){
final ProgressDialog ProgressDialog=新建ProgressDialog(MainActivity.this);
progressDialog.setMessage(“获取位置”);
progressDialog.setCancelable(假);
progressDialog.show();
FusedLocationProviderClient FusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(MainActivity.this);
if(ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS\u FINE\u LOCATION)!=PackageManager.permission\u已授予和&ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS\u LOCATION)!=PackageManager.permission\u已授予){
考虑到呼叫
//ActivityCompat#请求权限
//在此处请求缺少的权限,然后覆盖
//public void onRequestPermissionsResult(int-requestCode,字符串[]权限,
//int[]格兰特结果)
//处理用户授予权限的情况。请参阅文档
//对于ActivityCompat,请请求权限以获取更多详细信息。
返回;
}
Task Task=fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时的公共无效(位置){
如果(位置!=null)
{
String-lati=String.valueOf(location.getLatitude());
getLat.setText(lati);
String longi=String.valueOf(location.getLongitude());
getLat.setText(longi);
Toast.makeText(MainActivity.this,“找到位置!!!”,Toast.LENGTH\u LONG.show();
progressDialog.cancel();
}
其他的
Toast.makeText(MainActivity.this,“请启用GPS和Internet!!!”,Toast.LENGTH\u LONG.show();
}
});
}
}

我在我的一个Kotlin项目中使用了FusedLocation,我将在下面发布该方法

确保您的位置服务处于启用状态,并且已向用户请求访问位置的权限,否则无法检测到最后一个位置

fun getLocation(): Task<Location>? {
    val mFusedLocationProviderClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(dashboardActivity)
    if (ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    val location = mFusedLocationProviderClient.lastLocation
    location.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val currentLocation = task.result
            if (currentLocation != null) {
                latitude = currentLocation.latitude
                longitude = currentLocation.longitude
                dashboardActivity.getLatLong(latitude.toString(), longitude.toString()) //this methods calls the collector geo-location service
            } else {

                val builder = AlertDialog.Builder(dashboardActivity)
                builder.setTitle("Location On")

                builder.setPositiveButton("Yes") { dialog, which ->
                    val viewIntent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                    dashboardActivity.startActivity(viewIntent)
                }
                builder.setNegativeButton("No") { dialog, which ->
                    Toast.makeText(dashboardActivity, "Please enable location service", Toast.LENGTH_SHORT).show()
                }
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        }
    }
    return location
}
fun getLocation():任务?{
val mFusedLocationProviderClient:FusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(仪表板活动)
if(ActivityCompat.checkSelfPermission(仪表板活动,清单。权限。访问\u精细\u位置)!=授予PackageManager.permission和&ActivityCompat.checkSelfPermission(仪表板活动,清单。权限。访问\u粗略\u位置)!=授予PackageManager.permission){
}
val location=mFusedLocationProviderClient.lastLocation
location.addOnCompleteListener{task->
如果(任务成功){
val currentLocation=task.result
if(currentLocation!=null){
纬度=当前位置。纬度
经度=当前位置。经度
dashboardActivity.getLatLong(latitude.toString(),longitude.toString())//此方法调用收集器地理位置服务
}否则{
val builder=AlertDialog.builder(仪表板活动)
builder.setTitle(“上的位置”)
setPositiveButton(“Yes”){对话框,其中->
val viewIntent=Intent(android.provider.Settings.ACTION\u LOCATION\u SOURCE\u Settings)
仪表板活动。startActivity(viewIntent)
}
setNegativeButton(“No”){对话框,其中->
Toast.makeText(仪表板活动,“请启用位置服务”,Toast.LENGTH\u SHORT.show())
}
val对话框:AlertDialog=builder.create()
dialog.show()
}
}
}
返回位置
}

我没有使用
addOnSuccessListener()
而是使用了
addOnCompleteListener()
,我认为这可能是问题所在。

我在我的一个Kotlin项目中使用了FusedLocation,我将在下面发布该方法

确保您的位置服务处于启用状态,并且已向用户请求访问位置的权限,否则无法检测到最后一个位置

fun getLocation(): Task<Location>? {
    val mFusedLocationProviderClient: FusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(dashboardActivity)
    if (ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(dashboardActivity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
    }
    val location = mFusedLocationProviderClient.lastLocation
    location.addOnCompleteListener { task ->
        if (task.isSuccessful) {
            val currentLocation = task.result
            if (currentLocation != null) {
                latitude = currentLocation.latitude
                longitude = currentLocation.longitude
                dashboardActivity.getLatLong(latitude.toString(), longitude.toString()) //this methods calls the collector geo-location service
            } else {

                val builder = AlertDialog.Builder(dashboardActivity)
                builder.setTitle("Location On")

                builder.setPositiveButton("Yes") { dialog, which ->
                    val viewIntent = Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                    dashboardActivity.startActivity(viewIntent)
                }
                builder.setNegativeButton("No") { dialog, which ->
                    Toast.makeText(dashboardActivity, "Please enable location service", Toast.LENGTH_SHORT).show()
                }
                val dialog: AlertDialog = builder.create()
                dialog.show()
            }
        }
    }
    return location
}
fun getLocation():任务?{
val mFusedLocationProviderClient:FusedLocationProviderClient=LocationServices.getFusedLocationProviderClient(仪表板活动)
if(ActivityCompat.checkSelfPermission(仪表板活动,清单。权限。访问\u精细\u位置)!=授予PackageManager.permission和&ActivityCompat.checkSelfPermission(仪表板活动,清单。权限。访问\u粗略\u位置)!=授予PackageManager.permission){
}
val location=mFusedLocationProviderClient.lastLocation
location.addOnCompleteListener{task->
如果(任务成功){
val currentLocation=task.result
if(currentLocation!=null){
纬度=当前位置。纬度
经度=当前位置。经度
dashboardActivity.getLatLong(latitude.toString(),l