views:

3154

answers:

2

I am trying to design my first Android application with the use of GPS. As of right now, I have a drawable button that when clicked, alerts a Toast message of the longitude and latitude. I have tried to use the telnet localhost 5554 and then geo fix #number #number to feed in values but no results display just 0 0. I have also tried DDMS way of sending GPS coordinates and I get the same thing.

My question is what exactly is the correct way of using the geo fix and the DDMS way of sending coordinates. I have used Location, LocationManger and LocationListener but I am not sure which is the right choice.

Code is given, just in case if the error exists with the code

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button button = (Button) findViewById(R.id.track);
    button.setOnClickListener(this);
    LocationManager location =(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Location loc = location.getLastKnownLocation(location.GPS_PROVIDER);
    updateWithNewLocation(loc);

  }

 private final LocationListener locationListener = new LocationListener() {
   public void onLocationChanged(Location location) {
     updateWithNewLocation(location);
   }

  private void updateWithNewLocation(Location l) {
  longitude = l.getLongitude();
  latitude = l.getLatitude();
  provider = l.getProvider();     
  }

    public void onClick(View v) { 
     Toast.makeText(this, "Your location is " + 
      longitude + " and " + latitude + " provided by: " + 
      provider, Toast.LENGTH_SHORT).show();          
   }     
}
+2  A: 

Two things

1) Make latitude and longitude volatile

2) Try registering as a listener to the GPS location manager

location.requestLocationUpdates(LocationManager.GPS, 100, 1, locationListener);

That should get you callbacks sent to your locationListener object.

You can check out some sample Android GPS code here: http://www.devx.com/wireless/Article/43005

(WARNING! DevX has added a registration landing since I've added this link!)

haseman
The sample code leads to a site with a registration wall. How annoying.
Joe Ludwig
Yikes! That is annoying! Sorry about that, I think devX added the registration landing in the last month or so :-( Added a warning to the answer.
haseman
To get around the pay wall, paste the url into google search and then click on it. They can't show a different page to google than to you (through google).
Alan Jackson
+1  A: 

comment the line Location loc = location.getLastKnownLocation(location.GPS_PROVIDER);

And test the application by updating gps manually in ddms

if it works, come back to the code then put Location loc = location.getLastKnownLocation("gps");

Mamadou
I have tried doing as you suggested back when I had encountered the problem, although now its fixed and is not much of a problem anymore. The reason why it was not working was because I did not register a listener for my location updates. Thank you for replyng anyways.
Anthony Forloney