Mukunda
Mukunda

Reputation: 1623

Time taken by an android gps device to get its latitude and longitude

I am trying an code for finding latitude and longitude but takes lot of time to give coordinates or does not give the coordinates at all, what could be the problem. I am trying my code on Samsung Galaxy tab.

LocationManager mlocManager;
double lat,lng; 
 LocationListener mlocListener = new MyLocationListener();

I call the below code in onCreate() method

mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

Listener MyLocationListener class code is below

 public class MyLocationListener implements LocationListener

        {




        @Override
        public void onLocationChanged(Location loc){

        loc.getLatitude();

        loc.getLongitude();

        String Text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
        String latLongString = "";
        if (loc != null) {
            lat = loc.getLatitude();
            lng = loc.getLongitude();
            latLongString = "Lat:" + lat + "\nLong:" + lng;
            TextView myLocationText = (TextView)findViewById(R.id.FindUsTextView);
            myLocationText.setText("Your Current Position is:\n" + latLongString);
            System.out.println(latLongString);

        } 
       /* findUsProgressbarField.setVisibility(View.INVISIBLE);*/
        Toast.makeText( FindUs.this, Text, Toast.LENGTH_SHORT).show();
        mlocManager.removeUpdates(mlocListener);

        }

        @Override

        public void onProviderDisabled(String provider){

        Toast.makeText( getApplicationContext(),"Gps Disabled, Please Enable the GPS", Toast.LENGTH_SHORT ).show();

        }

        @Override

        public void onProviderEnabled(String provider){

        Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();

        }

        @Override

        public void onStatusChanged(String provider, int status, Bundle extras){

        }



        }
        @Override
        protected void onDestroy() {
        super.onDestroy();
        if (mlocManager != null) {
            mlocManager.removeUpdates(mlocListener);
            mlocManager = null;
        }
        }

What could be the problem, any pointers or have I not considered some points. Looking forward to your reply. thanks.

Upvotes: 0

Views: 427

Answers (4)

M. le Rutte
M. le Rutte

Reputation: 3563

Getting a fix for a GPS may take quite a while, it depends on many variables, such as the location of the GPS satelites and whether your GPS unit has a clear view to the satellites. As the GPS signal is quite weak it is often blocked out by buildings.

As such, this is something your users will encounter too, so be sure to make your app usable too if it doesn't get a fix, or gets a fix slowly.

While developing you may feed the device fake locations, but you'll need to enable 'Allow fake locations' at the developer's options of your device. This will speed up development.

Upvotes: 0

Arun Paarthi
Arun Paarthi

Reputation: 643

I also had the same problem and begin to think that something is wrong with the GPS or with the program. Then I tried it out side my office and it worked perfectly. Try using your mobile in an open space or near a window :)

cheers.

Upvotes: 1

Ifor
Ifor

Reputation: 2835

If your testing inside like most people are while developing this is fairly standard. GPS's like to see the sky. Try sitting next to a good big window. It helps me.

Upvotes: 1

NickT
NickT

Reputation: 23873

If the Galaxy Tab has A-GPS (assisted GPS) capabilty, i.e. if there's an option amonst the device's 'Location and Secuirity' settings of 'Use wireless networks', then enabling this will help get a lock quicker, even if you don't use the NETWORK_PROVIDER option in your code.

This is because the ephemeris and almanac data which tells the device what satellites ought to be in view at your (approximate) location at the time is transmitted via the phone network.

Upvotes: 1

Related Questions