简体   繁体   English

无法在Android中获得经度和纬度

[英]Unable to get longitude and latitude in android

I am developing android app to get my current longitude and latitude from GPS. 我正在开发android应用,以从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). 我不确定何时将其放入手机(Galaxy Nexus)。 I saw a GPS icon on the bar located at the top of my phone. 我在手机顶部的栏中看到一个GPS图标。 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. 我只是在没有移动数据的情况下打开wifi。 It seems that onLocationChanged is not invoked. 似乎未调用onLocationChanged。

Thanks everyone! 感谢大家!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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