无法在Android中获得经度和纬度 [英] Unable to get longitude and latitude in android
问题描述
我正在开发android应用,以从GPS获取当前的经度和纬度. 我有以下代码
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
}
}
我不确定何时将其放入手机(Galaxy Nexus).我在手机顶部的栏中看到一个GPS图标.但是,文本字段不会更新以显示我当前的经度和纬度.我想问一下这是我的密码还是手机的问题?我只是在没有移动数据的情况下打开wifi.似乎未调用onLocationChanged.
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.
谢谢大家!
推荐答案
使用GPS很难在房间/公寓内长时间放置杂物.代替LocationManager.GPS_PROVIDER使用LocationManager.NETWORK_PROVIDER ..它将解决您的问题.
Its difficult to get Lat,Long inside room/apartment using GPS.Instead of LocationManager.GPS_PROVIDER use LocationManager.NETWORK_PROVIDER..it ll solve ur problem.
这篇关于无法在Android中获得经度和纬度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!