1

我一直在努力获取我当前位置的 GPS 坐标,但我的应用程序从不锁定 GPS 卫星。

通知区域中的 GPS 图标一直闪烁。

而我尝试在 Android 上使用谷歌地图(预安装的应用程序)并且该东西能够在大约 60 秒内锁定!在这两种情况下,我的 3G 数据连接都已打开并正常工作。

更新:我使用的是 Android 2.3.3(HTC Desire S(工厂安装的操作系统;未应用更新))Logcat 输出在这里。现在这没有设置LocationUpdates()更新到 0、0 之间的最小时间和最小距离。

更新 #2:我之前的代码在这里(PasteBin 链接)。

更新#3:现在,在显示 Toast .."Available".. in on 后,我正在强制关闭onStatusChanged()

更新#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;
        }
    }

}

请回答,因为这对我来说非常紧迫,任何帮助都是非常可观的:)

谢谢..

4

3 回答 3

1

很明显,您不会获得任何位置更新,因为您已将 minDistance 设置为 50 米或 164.042 英尺。您似乎将其与准确性混淆了。

minDistance参数是位置更新之间的最小距离。因此,您必须至少移动 50 米才能获得位置更新。

还要确保您可以清楚地看到天空,以便获得 GPS 信号。

在文档中阅读更多信息

http://developer.android.com/reference/android/location/LocationManager.html

于 2013-02-04T19:14:17.460 回答
1

我在这里问了一个相关的问题。

在我看来,您手机上的 GPS 无法看到卫星。您需要让我们的办公室/家中露天。就我而言,我搬到了NetworkProviderGPS 太笨拙了。

另请注意,您为距离和时间提供的参数不是文字,它best guess是 api 制作的。所以不要指望 API 回调的响应时间/距离。

于 2013-02-05T07:04:14.480 回答
0

您是否拥有所有这些清单权限?

ACCESS_COARSE_LOCATION

ACCESS_FINE_LOCATION

ACCESS_LOCATION_EXTRA_COMMANDS

ACCESS_MOCK_LOCATION

CONTROL_LOCATION_UPDATES

INTERNET
于 2013-02-04T17:39:00.580 回答