我想在用户单击按钮时获得当前的纬度和经度。为此,我编写了以下代码:

LocationManager mlocManager=null;
                        LocationListener mlocListener;
                        mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                        mlocListener = new MyLocationListener();
                        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
                        if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                            Log.v("this",MyLocationListener.latitude+ " lat");
                            if(MyLocationListener.latitude>0){
                                in = new Intent(DrawerActivity.this, Barbers.class);
                                in.putExtra("w", "nearest");
                                in.putExtra("latitude",Double.toString(MyLocationListener.latitude));
                                in.putExtra("longitude",Double.toString(MyLocationListener.longitude));
                                startActivity(in);
                            }else{
                                MyToast.makeText(DrawerActivity.this, Z_Farsi.Convert(getString(R.string.gpsfinding)));
                            }
                        } else {
                            MyToast.makeText(DrawerActivity.this, Z_Farsi.Convert(getString(R.string.gpsoffline)));
                        }


GPS开启,当前位置完美显示在google map上。

问题是什么 ?为什么返回0.0?

如何解决呢?

最佳答案

getLastKnownLocation丢失。您应该有这样的东西:

Location location = null;
if (mlocManager!= null) {
                    location = mlocManager
                         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }

关于android - android -LocationManager返回0.0作为纬度和经度,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37314929/

10-12 02:42