您的位置:首页 > 移动开发 > Android开发

Android手机定位完整代码

2012-02-01 22:12 489 查看
public class LocationClient extends Activity implements LocationListener {

private static final String TAG = LocationClient.class.getSimpleName();

private static final String[] S = { "Out of Service",

"Temporarily Unavailable", "Available" };

private TextView output;

private LocationManager locationManager;

private String bestProvider;

private Geocoder geocoder;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Get the output UI

output = (TextView) findViewById(R.id.output);

// Get the location manager

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

geocoder = new Geocoder(this);

// List all providers:

List<String> providers = locationManager.getAllProviders();

for (String provider : providers) {

printProvider(provider);

}

System.out.print("The providers: " + providers);

Criteria criteria = new Criteria();

bestProvider = locationManager.getBestProvider(criteria, false);

output.append("\n\nBEST Provider:\n");

printProvider(bestProvider);

output.append("\n\nLocations (starting with last known):");

Location location = locationManager.getLastKnownLocation(bestProvider);

printLocation(location);

}

/** Register for the updates when Activity is in foreground */

@Override

protected void onResume() {

super.onResume();

locationManager.requestLocationUpdates(bestProvider, 2000, 1, this);

}

/** Stop the updates when Activity is paused */

@Override

protected void onPause() {

super.onPause();

locationManager.removeUpdates(this);

}

public void onLocationChanged(Location location) {

printLocation(location);

}

public void onProviderDisabled(String provider) {

// let okProvider be bestProvider

// re-register for updates

output.append("\n\nProvider Disabled: " + provider);

}

public void onProviderEnabled(String provider) {

// is provider better than bestProvider?

// is yes, bestProvider = provider

output.append("\n\nProvider Enabled: " + provider);

}

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

output.append("\n\nProvider Status Changed: " + provider + ", Status="

+ S[status] + ", Extras=" + extras);

}

private void printProvider(String provider) {

LocationProvider info = locationManager.getProvider(provider);

Log.d(TAG, "Name:" + info.getName());

Log.d(TAG, "Accuracy:" + info.getAccuracy());

Log.d(TAG, "Require Cell? " + info.requiresCell());

Log.d(TAG, "Require Network? " + info.requiresNetwork());

Log.d(TAG, "Require Satellite? " + info.requiresSatellite());

Log.d(TAG, "Supports Altitude? " + info.supportsAltitude());

Log.d(TAG, "Supports Bearing? " + info.supportsBearing());

Log.d(TAG, "Supports Speed? " + info.supportsSpeed());

Log.d(TAG, "Power requirement?" + info.getPowerRequirement());

Log.d(TAG, "Might steal my money?"+ info.hasMonetaryCost());

output.append(info.toString() + "\n\n");

}

private void printLocation(Location location){

if (location == null)

output.append("\nLocation[unknown]\n\n");

else

{

String text = String.format("Latitude:\t %f \nLongitude:\t %f\n Altitude:\t %f\n Bearing:\t %f\n",

location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getBearing());

Log.d(TAG, text);

output.append("\n\n"+text);

}

try{

List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),

location.getLongitude(), 10);

for(Address address: addresses)

{

output.append("\n"+ address.getAddressLine(0));

}

}catch(Exception e)

{

Log.e("WhereAmI", "Couldn't get Geocoder data", e);

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: