开发者

Android GPS cloud of confusion!

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 locationListen开发者_运维知识库er = 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();          
   }     
}


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!)


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");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜