【问题标题】:Fused Location Provider Client always returns first location as nullFused Location Provider Client 始终将第一个位置返回为 null
【发布时间】:2020-03-30 23:47:52
【问题描述】:

我有一个带有按钮的活动,单击该按钮时,应显示该位置。但是第一次点击总是返回null,第二次点击返回正确的信息。我尝试使用延迟,但这只会延迟警报对话框的创建,第一次单击按钮时的第一个值仍然为空。

public class MainActivity extends AppCompatActivity {
    private FusedLocationProviderClient fusedLocationClient;
    private Double longitude, latitude;
    private Float accuracy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        fusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

        Button testButton = findViewById(R.id.test_button);
        testButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getCurrentLocation();
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Current Location")
                        .setMessage("Location: " + latitude +", "+ longitude +
                                "\nAccuracy: " + String.format(Locale.getDefault(),"%.2f", accuracy) +"m")
                        .setNegativeButton(android.R.string.cancel, null)
                        .show();
                }
            }
        });
    }

    private void getCurrentLocation() {
    fusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        // Get last known location, and check for null.
                        if(location!=null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            accuracy = location.getAccuracy();
                        }
                    }
                });
    }

【问题讨论】:

    标签: android android-location


    【解决方案1】:

    尝试使变量受保护,这样做:

     protected FusedLocationProviderClient fusedLocationClient;
     protected Double longitude, latitude;
     protected Float accuracy;
    

    取而代之的是:

    private FusedLocationProviderClient fusedLocationClient;
    private Double longitude, latitude;
    private Float accuracy;
    

    【讨论】:

    • 不,没有任何改变。还是同样的问题。
    【解决方案2】:

    在 Location 可用后构建 AlertDialog,如下所示:

    private void getCurrentLocation() {
        fusedLocationClient.getLastLocation()
                .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        // Get last known location, and check for null.
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                            accuracy = location.getAccuracy();
                            new AlertDialog.Builder(context)
                                    .setTitle("Current Location")
                                    .setMessage("Location: " + latitude + ", " + longitude +
                                            "\nAccuracy: " + String.format(Locale.getDefault(), "%.2f", accuracy) + "m")
                                    .setNegativeButton(android.R.string.cancel, null)
                                    .show();
    
                        }
                    }
                });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-30
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      相关资源
      最近更新 更多