测试开发者手机上的 GPS 坐标

发布于 2024-11-26 09:43:59 字数 1605 浏览 2 评论 0原文

我的代码是在给定时间获取某个位置的坐标,然后显示纬度/经度坐标。我认为我的代码是正确的,但问题出在给手机授予 GPS 权限。我使用的是 Samsung Nexus S,并且安装了所有正确的驱动程序,但手机上没有网络。我被告知 GPS 应该仍然可以工作并且应该能够检索坐标。我启用了“使用 GPS 卫星”设置,并在清单中授予了权限,如清单.xml 中所示,

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> 

我输入了所有可能的位置权限,以防万一我丢失了某些内容,希望这不是一个问题。

至于我的代码,我在 onCreate 方法(类?我不确定它的正确名称)中有这个

    double[] gps = getGPS();
    TextView tv = (TextView) this.findViewById(R.id.gpsCoordinates);
    tv.setText("Latitude: " + gps[0] + "\nLongitude: " + gps[1]);

,然后在 GPS 类中使用一个私有方法来实际获取坐标

    private double[] getGPS() {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        List<String> providers = lm.getProviders(true);

        /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
        Location l = null;

        for (int i=providers.size()-1; i>=0; i--) {
                l = lm.getLastKnownLocation(providers.get(i));
                if (l != null) break;
        }

        double[] gps = new double[2];
        if (l != null) {
                gps[0] = l.getLatitude();
                gps[1] = l.getLongitude();
        }
        return gps;

,我相信我从 stackoverflow 和一些代码中提取了这段代码其他论坛,然后稍微调整了一下。

My code is taking coordinates of a position at one given time, and then displaying Lat/Long coordinates. I think my code is right, but where the problem lies is giving GPS permission to the phone. I am using a Samsung Nexus S, and I have all proper drivers installed, but there is no network on the phone. I was told that GPS should still work and should be able to retrieve coordinates. I have the, "Use GPS Satellites" setting enabled, and have given the permission in the manifest, as seen here in the manifest.xml

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission> 

I threw in all possible location permissions just in case I was missing something, hopefully that isn't a problem.

As for my code I have this in the onCreate method (class? I'm not sure of its proper name)

    double[] gps = getGPS();
    TextView tv = (TextView) this.findViewById(R.id.gpsCoordinates);
    tv.setText("Latitude: " + gps[0] + "\nLongitude: " + gps[1]);

And then a private method in the GPS class to actually get the coordinates

    private double[] getGPS() {
        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
        List<String> providers = lm.getProviders(true);

        /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
        Location l = null;

        for (int i=providers.size()-1; i>=0; i--) {
                l = lm.getLastKnownLocation(providers.get(i));
                if (l != null) break;
        }

        double[] gps = new double[2];
        if (l != null) {
                gps[0] = l.getLatitude();
                gps[1] = l.getLongitude();
        }
        return gps;

I believe I pulled this code off of stackoverflow and a few other forums and then tweaked it a bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请恋爱 2024-12-03 09:43:59
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.LOCATION"></uses-permission>

添加以上权限。也许这会对您有所帮助。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.LOCATION"></uses-permission>

Add above permissions.May be this will help you.

乖不如嘢 2024-12-03 09:43:59

您需要添加以下权限

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 

Ref:
http://developer.android.com/reference/android/Manifest.permission .html#ACCESS_FINE_LOCATION

You need to put the following permission

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> 

Ref:
http://developer.android.com/reference/android/Manifest.permission.html#ACCESS_FINE_LOCATION

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文