0

我正在创建一项服务,该服务将每 5 分钟更新一次用户位置。我正在使用 DDMS 将坐标发送到模拟器。我想转换坐标并找到位置。我怎样才能做到这一点?我是android新手请帮忙。到目前为止,这是我的代码

 public class GetLocationService extends Service {
protected LocationManager locationManager;
Button start;

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    LocationListener ll = new MyLocListener();
    Location location = new Location("abc");
    ll.onLocationChanged(location ); 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, ll);
    return START_STICKY;
}

 private class MyLocListener implements LocationListener {
public void onLocationChanged(Location location) {
    if (location != null) {
    Log.d("LOCATION CHANGED", location.getLatitude() + "");
    Log.d("LOCATION CHANGED", location.getLongitude() + "");
    }
    Toast.makeText(GetLocationService.this,
    location.getLatitude() + "" + location.getLongitude(),
    Toast.LENGTH_LONG).show();

    }
  }

}
4

1 回答 1

1

尝试这个:

try{
 Geocoder geo = new Geocoder(youractivityclassname.this.getApplicationContext(), Locale.getDefault());
 List<Address> addresses = geo.getFromLocation(latitude, longitude, 1);
  if (addresses.isEmpty()) {
        yourtextfieldname.setText("Waiting for Location");
  }
  else {
     if (addresses.size() > 0) {       
        Log.d(TAG,addresses.get(0).getFeatureName() + ", 
         " + addresses.get(0).getLocality() +", 
         " + addresses.get(0).getAdminArea() + ",
         " + addresses.get(0).getCountryName());

     }
  }
}
catch (Exception e) {
    e.printStackTrace(); 
}
于 2012-07-10T07:18:19.910 回答