0

我想在当前位置的纬度和经度。它在模拟器上运行,但在移动设备上提供 lat/long 0.0。我的设备是 LG 4x 高清。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;
}

mainfest 权限:

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

lat long 在设备上永远是 0.0,但它在模拟器上工作。它是一个简单的代码,为什么它不起作用?我哪里错了?

4

1 回答 1

0
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);-->may be here you get null value of location manager replace with this line  
lm = (LocationManager) 
            context.getSystemService(Context.LOCATION_SERVICE);
于 2013-09-19T15:29:54.967 回答