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);
}
}
}
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);
}
}
}
相关文章推荐
- 代码实现获取android手机定位
- Android手机定位案例代码
- Android 编程代码-手机定位
- Android:百度地图定位,完整代码奉上
- 第二十期 在Android中修改GPS定位数据的完整方案《手机就是开发板》
- Android关于 手机返回键和代码finish()方法退出activity他们有什么区别;当activity使用共享动画时应该调用哪个方法,结束activity
- android手机定位显示信息
- Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)
- 28手机定位代码的实现
- android手机短信监听器,监听到的短信发送到web服务器,完整源码分享
- 如何定位联芯平台的Android系统代码第三方apps
- 基于android手机的3G+GPS远程控制模型车工程-android手机编程7-伪视频控制远程手机端程序(代码篇)
- 基于Android的淘宝网手机客户端开发见解(配完整视频教程讲解)
- android 7.0 百度地图定位总结 手机硬件-android 4.4升级到7.0
- android手机定位
- Android 实现语音识别的完整代码
- Android JNI 使用的数据结构JNINativeMethod详解 ||建立Android SDK下的JNI、JAVA应用完整步骤---Android JAVA调用C++代码
- 用代码实现断开Android手机USB连接
- Android获取当前手机网络类型(2g、3g、4g、wifi)以及手机型号、版本号代码
- Android学习之使用手机网络和GPS定位