Rob Jarvis
Rob Jarvis

Reputation: 356

GPS Coordinates on Test Developer Phone

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.

Upvotes: 2

Views: 804

Answers (2)

Ravi Bhatt
Ravi Bhatt

Reputation: 1948

<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.

Upvotes: 1

Suchi
Suchi

Reputation: 10039

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

Upvotes: 0

Related Questions