1

ユーザーがボタンをクリックしたときに現在の緯度と経度を取得したい。そうするために、私はこれらのコードを書きました:

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マップに完全に表示されています。

何が問題ですか ?なぜ 0.0 を返すのですか?

これを解決するには?

4

4 に答える 4

1

getLastKnownLocation不足している。次のようなものが必要です。

Location location = null;
if (mlocManager!= null) {
                    location = mlocManager
                         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
于 2016-05-19T06:21:03.013 に答える
0

このようにしてみてください

manager.requestLocationUpdates("gps", 10, 10, new LocationListener() {
@Override
public void onLocationChanged(Location location) {

 double lat=location.getLatitude();
 double lon=location.getLognitude()

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}
});
于 2016-05-19T06:21:23.140 に答える