Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Getting location - android

  • 21-11-2011 7:59pm
    #1
    Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭


    Hi, I am working on getting an androids GPS coordinates. I have this code below which was working for me last week, however it is not this week.
    I know that: locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); is returning null, but i'm not sure why, when it was working before. also if i exclude this then my app does nothing(does not get any GPS updates)
    private TextView locationView;
        private LocationListener locationListener;
        private String coordinates;
        
         /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState){
            Log.d(TAG, "START");
            
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
            locationView = (TextView) findViewById(R.id.locationView);
            
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
               
            // ask the Location Manager to send us location updates
            locationListener = new MyLocationListener();        
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 60000, 10.0f, locationListener);  
            
            coordinates = Double.toString(location.getLatitude()) +", "+ Double.toString(location.getLongitude());     
            locationView.setText(coordinates);
        }
          
        private class MyLocationListener implements LocationListener{
            public void onLocationChanged(Location location){
                coordinates = Double.toString(location.getLatitude()) +", "+ Double.toString(location.getLongitude());
                locationView.setText(Double.toString(location.getLatitude()));
                //sendLocation(coordinates);
            }
            
            public void onProviderDisabled(String provider) {}
            public void onProviderEnabled(String provider) {}
            public void onStatusChanged(String provider, int status, Bundle extras) {}
        }
    
    permissions:
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    I should point out that when i exclude the getLastKnownLocation() call, i can see the GPS symbol in the top right corner of the screen. when i include it, the app doesn't even run


Comments

  • Registered Users, Registered Users 2 Posts: 1,235 ✭✭✭Odaise Gaelach


    Are you sure that you have the GPS enabled on your phone? I think if you go to the home screen, tap the Menu button and navigate to Settings then Security and Location and make sure that "Enable GPS Satellites" is checked then you should be good to go. Remember that GPS might take a minute or two to get your position, and may not at all if you're indoors.

    However, I think there are a few things that I notice from your code.
    private class MyLocationListener implements LocationListener
    ...
                locationView.setText(Double.toString(location.getLatitude()));
    

    A small oversight I think, but did you mean to use locationView.setText(coordinates) instead?


    I would also recommend something similar for the three other methods in MyLocationListener:
    public void onProviderDisabled(String provider)
    {
         locationView.setText(provider + " disabled.");
    }
    
    public void onProviderEnabled(String provider)
    {
         locationView.setText(provider + " enabled.");
    }
    
    public void onStatusChanged(String provider, int status, Bundle extras)
    {
         String statusMessage = "";
    
         switch (status)
         {
              case 0:
                statusMessage = "Out of service";
                break;
    
              case 1:
                statusMessage = "Temporarily unavailable";
                break;
    
              case 2:
                statusMessage = "Available";
                break;
    
              default:
                statusMessage = "Unknown";
                break;
         }
         locationView.setText(provider + " status: " + statusMessage);
    }
    

    With those three methods filled in, you'll be able to see your GPS status when you're out and about testing your app. :)


  • Registered Users, Registered Users 2 Posts: 1,180 ✭✭✭EyeSight


    thank you very much. i can't believe i missed that. its so typical that i'd spend hours trying to debug this and "fresh eyes" find it instantly :p


Advertisement