简体   繁体   中英

Unable to get longitude and latitude in android

I am developing android app to get my current longitude and latitude from GPS. I have the following code

public class setting extends Activity {

TextView longval;
TextView latval;

@Override
public void onCreate(Bundle savedInstanceState){
/** Called when the activity is first created. */
    super.onCreate(savedInstanceState);
    setContentView(R.layout.setting);

    longval = (TextView)findViewById(R.id.longitudefield);
    latval = (TextView)findViewById(R.id.latitudefield);

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

//Location lastLocation = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);
LocationListener locationListener = new myLocationListener();

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
class myLocationListener implements LocationListener{

    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        if(location != null){
            double longitude = location.getLongitude();
            double latitude = location.getLatitude();
            //Toast.makeText(getApplicationContext(), Double.toString(longitude), Toast.LENGTH_SHORT).show();
            longval.setText(Double.toString(longitude));
            latval.setText(Double.toString(latitude));
        }
    }

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

    }

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

    }

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

    }

}

I am not sure when I put it into my phone (Galaxy Nexus). I saw a GPS icon on the bar located at the top of my phone. However, the textfield is not updated to show my current longitude and latitude. I would like to ask if it is the problem of my code or my phone? I am just switching on wifi without mobile data. It seems that onLocationChanged is not invoked.

Thanks everyone!

使用GPS难以在房间/公寓内长时间,长时间地放置。长期使用LocationManager.NETWORK_PROVIDER代替LocationManager.GPS_PROVIDER ..它将解决您的问题。

您的密码是正确的。将其安装在设备上后,请更改当前位置至少10米。我们从当前房间转到距离至少10米的另一个房间。希望您能获得输出....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM