java - 无法获取当前 GPS 位置坐标

标签 java android google-maps android-maps google-maps-mobile

我一直在努力获取当前位置的 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/

相关文章:

执行 Ghost4J 时出现 java.lang.IllegalStateException(Linux 32 位与 64 位)

java - Vert.x Java 中异步共享映射的删除方法出现异常

java - Android - 如何将我自己的音频编解码器添加到 AudioRecord?

google-maps - 禁用 Google map 嵌入中的 map 控件

java - NO_MODIFICATION_ALLOWED_ERR : An attempt is made to modify an object where modifications are not allowed

android - 无需身份验证即可获取 Twitter 主页时间线(仅应用程序身份验证)

android - Google Play 商店更新是否会重新下载整个应用程序?

android - 尝试从 Firebase 存储加载图像时出现 403 Forbidden 错误

ios - Cordova iOs Google Maps map Appstore 上空白

javascript - Google Maps API V3 双指缩放不适用于 Windows 8.1 预览版上的 IE 11