0

現在地の緯度と経度を知りたいです。エミュレーターで動作しますが、モバイル デバイスでは緯度/経度 0.0 になります。私のデバイスはLG 4x hdです。src/.java ファイル:

private TextView txt_lat,txt_lon,txt_gps;
private LocationManager lm;
double lat, lon;
protected LocationListener locatlist;
Location locat;
GpsStatus.Listener gpslist;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt_lat= (TextView) findViewById(R.id.textView2);
    txt_lon= (TextView) findViewById(R.id.textView4);
    txt_gps = (TextView) findViewById(R.id.textView5);
            lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locat = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if(locat != null) {
                lat = locat.getLatitude();
                lon = locat.getLongitude();
            }
            locatlist = new LocationListener() {

                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderEnabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProviderDisabled(String provider) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onLocationChanged(Location location) {
                    // TODO Auto-generated method stub
                    lat = location.getLatitude();
                    lon = location.getLongitude();
                    txt_lat.setText("lat : "+lat);
                    txt_lon.setText("lon : "+lon);
                }
            };
            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locatlist);
            if(lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                txt_gps.setText("GPS is started");
            }
            else if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){
                txt_gps.setText("GPS is non-start");
            }
            txt_lat.setText("lat : "+lat);
            txt_lon.setText("lon : "+lon);
} 

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

メインフェストの許可:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

デバイスでは lat long は永久に 0.0 ですが、エミュレータでは動作します。これは単純なコードですが、なぜ機能しないのですか? どこが間違っているのですか?

4

1 に答える 1