首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >FusedLocationProviderClient未返回位置

FusedLocationProviderClient未返回位置
EN

Stack Overflow用户
提问于 2019-03-28 19:07:24
回答 1查看 1.9K关注 0票数 1

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

代码如下:

代码语言:javascript
复制
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();
            }
        });
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-03-28 19:17:35

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

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

代码语言:javascript
复制
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
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55396046

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档