我一直在努力获取当前位置的 GPS 坐标,但我的应用从未锁定到 GPS 卫星。
通知区域中的 GPS 图标一直在闪烁。
而我尝试在 Android 上使用 Google map (预装应用程序),那东西能够在大约 60 秒内锁定!在这两种情况下,我的 3G 数据连接都已打开并正常工作。
更新:我使用的是 Android 2.3.3(HTC Desire S(出厂安装操作系统;未应用更新))Logcat 输出为 here .现在,无需将 LocationUpdates()
的最小时间和更新之间的最小距离设置为 0、0。
更新 #2: 我之前的代码是 here (PasteBin 链接)。
更新 #3: 现在,在 onStatusChanged()
中显示 Toast .."Available".. 后,我正在强制关闭。
更新#4:终于..我让它工作了。
--
那么,谷歌地图的应用程序是否使用了一些专有代码来锁定 GPS 信号?我尝试过使用各种版本的代码。尝试使用标准,但从未让它们与 GPS 一起使用。对我而言,必须通过 GPS 获得精确(~50 英尺精度)的位置坐标。
我的代码:
public class LocationDemoActivity extends Activity implements LocationListener {
LocationManager locationManager;
StringBuilder builder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000l, 50.0f, this);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationManager.removeUpdates(this);
locationManager = null;
Intent i = new Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
@Override
public void onLocationChanged(Location location) {
// builder = new StringBuilder();
double lati=location.getLatitude();
double longi=location.getLongitude();
double alti=location.getAltitude();
float acc=location.getAccuracy();
float speed=location.getSpeed();
long time=location.getTime();
System.out.println(lati);
System.out.println(longi);
System.out.println(alti);
System.out.println(acc);
System.out.println(speed);
System.out.println(time);
/*Toast.makeText(this, "Lati: " + lati,Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Long: " + longi,Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Alti: " + alti,Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Acc.: " + acc,Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Speed: " + speed,Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Time: " + time,Toast.LENGTH_SHORT).show();*/
builder.append("Longitide: " + location.getLongitude());
builder.append('\n');
builder.append("Latitude: " + location.getLatitude());
builder.append('\n');
builder.append("Altitude: " + location.getAltitude());
builder.append('\n');
builder.append("Accuracy: " + location.getAccuracy());
builder.append('\n');
builder.append("TimeStamp:" + location.getTime());
builder.append('\n');
System.out.println(builder.toString());
Toast.makeText(this, builder.toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onProviderDisabled(String provider) {
System.out.println("Provider Disabled:");
Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
@Override
public void onProviderEnabled(String provider) {
System.out.println("Provider Enabled:");
Toast.makeText(this, "GPS is now enabled...", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.OUT_OF_SERVICE:
System.out.println("Status Changed: Out of Service");
Toast.makeText(this, "Status Changed: Out of Service",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.TEMPORARILY_UNAVAILABLE:
System.out.println("Status Changed: Temporarily Unavailable");
Toast.makeText(this, "Status Changed: Temporarily Unavailable",
Toast.LENGTH_SHORT).show();
break;
case LocationProvider.AVAILABLE:
System.out.println("Status Changed: Available");
Toast.makeText(this, "Status Changed: Available",
Toast.LENGTH_SHORT).show();
break;
}
}
}
请务必回答,因为这对我来说非常紧急,非常感谢任何帮助 :)
谢谢..
最佳答案
很明显您不会获得任何位置更新,因为您已将 minDistance 设置为 50 米或 164.042 英尺。看来您混淆了它与准确性。
minDistance
参数是位置更新之间的最小距离。因此,您必须至少移动 50 米才能获得位置更新。
还要确保您能清楚地看到天空,以便获得 GPS 信号。
在文档中阅读更多内容
http://developer.android.com/reference/android/location/LocationManager.html
关于java - 无法获取当前 GPS 位置坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14692202/