tags:

views:

230

answers:

2

I am trying to get the GPS location of my HTC magic using the following code :

public class TestGPS extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    final TextView tv1 = (TextView)findViewById(R.id.tv1);
    final TextView tv2 = (TextView)findViewById(R.id.tv2);
    LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener()
    {

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

        public void onProviderEnabled(String provider)
        {
        }

        public void onProviderDisabled(String provider)
        {
        }

        public void onLocationChanged(Location location)
        {
            tv1.setText(String.valueOf(location.getLatitude()));
            tv2.setText(String.valueOf(location.getLongitude()));
        }
    });
}

}

I waited for 5 minutes but none of the listener methods were called. The GPS icon shows up, stays there for some time but for some reason I don't get a fix, even outdoors in bright sunlight. I am testing on a HTC Magic with 1.6 SDK.

Can someone please tell me what's wrong with the code? Thanks.

A: 

The code looks fine. Make sure you have the ACCESS_FINE_LOCATION permission. Make sure your GPS is not broken -- in other words, does the built-in Maps application know where you are? Make sure your application works on the emulator with a fix you supply via the emulator.

Here is a sample project that uses GPS and that works fine on hardware.

CommonsWare
permissions are OK and the built-in Maps application works fine...
Arutha
A: 

I've got a problem related to this too. I'm creating an application which needs the longitude and latitude of the user. Several example codes for obtaining the user location aren't working. The only difference to Arutha's problem: When using an emulator with an image of a real device (and so including google maps), even google maps can't find my location when sending a fixed geolocation to the device.

someprogrammerfromhere