【问题标题】:FusedLocationProviderClient not returning locationFusedLocationProviderClient 不返回位置
【发布时间】:2019-08-19 03:10:42
【问题描述】:

我正在使用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();
            }
        });
    }
}

【问题讨论】:

  • 我认为你的 OnSuccessListener 没有打电话。试试this
  • 您是否检查过您没有收到此错误Google Maps Android API: Authorization failure
  • 不,我没有收到任何错误。当我单击按钮时,进度对话框会出现并永远保持这种状态。

标签: android geolocation android-fusedlocation


【解决方案1】:

我在我的一个 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
}

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

【讨论】:

  • 获取位置是否需要很长时间?
  • 你能帮我检查一下吗,打开谷歌地图,让地图找到你,然后再回到你的应用程序
  • 你是在设备还是模拟器上测试?
  • 设备。我正在考虑向您发送我正在使用的代码,但我似乎无法在 cmets 中执行此操作。
猜你喜欢
  • 1970-01-01
  • 2023-01-17
  • 2017-12-10
  • 2019-05-09
  • 1970-01-01
  • 2018-06-08
  • 1970-01-01
  • 1970-01-01
  • 2018-12-24
相关资源
最近更新 更多