2010.12.10——— android 定位跟踪

2010.12.10——— android 定位跟踪

需求 :
获得手机的坐标 实时不断的显示在应用里


[b]Activity.java[/b]


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

public class MainActivity extends Activity {
private LocationManager locationManager;
private TextView tv;
//private StringBuilder builder = new StringBuilder("位置信息:\n");

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
tv = (TextView) findViewById(R.id.myTextView01);

String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);

//updateMsg(location);

LocationListener ll = new LocationListener(){
public void onLocationChanged(Location l) {
//updateMsg(l);
StringBuilder builder = new StringBuilder("位置信息:\n");
if (l != null) {
double lat = l.getLatitude();
double lng = l.getLongitude();
builder.append("(");
builder.append(lat);
builder.append(",");
builder.append(lng);
builder.append(")");

if(l.hasAccuracy()){
builder.append("\n精度:");
builder.append(l.getAccuracy());
}

if(l.hasAltitude()){
builder.append("\n高度:");
builder.append(l.getAltitude());
}

if(l.hasBearing()){
builder.append("\n方向:");
builder.append(l.getBearing());
}

if(l.hasSpeed()){
builder.append("\n速度:");
builder.append(l.getSpeed());
}

builder.append("\n");

} else {
builder.append("没有位置信息");
}
tv.setText(builder);
}
public void onProviderDisabled(String provider) {
}

public void onProviderEnabled(String provider) {
}

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

locationManager.requestLocationUpdates(provider, 500, 5,
ll);

}

// private void updateMsg(Location l) {
// StringBuilder builder = new StringBuilder("位置信息:\n");
// if (l != null) {
// double lat = l.getLatitude();
// double lng = l.getLongitude();
// builder.append("(");
// builder.append(lat);
// builder.append(",");
// builder.append(lng);
// builder.append(")");
//
// if(l.hasAccuracy()){
// builder.append("\n精度:");
// builder.append(l.getAccuracy());
// }
//
// if(l.hasAltitude()){
// builder.append("\n高度:");
// builder.append(l.getAltitude());
// }
//
// if(l.hasBearing()){
// builder.append("\n方向:");
// builder.append(l.getBearing());
// }
//
// if(l.hasSpeed()){
// builder.append("\n速度:");
// builder.append(l.getSpeed());
// }
//
// builder.append("\n");
//
// } else {
// builder.append("没有位置信息");
// }
// tv.setText(builder);
// }

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

// 按下键盘上返回按钮
if (keyCode == KeyEvent.KEYCODE_BACK) {

new AlertDialog.Builder(this)
.setMessage("确定退出系统吗?")
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
})
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
finish();
}
}).show();

return true;
} else {
return super.onKeyDown(keyCode, event);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
// 或者下面这种方式
//System.exit(0);
//建议用这种
android.os.Process.killProcess(android.os.Process.myPid());
}


}


最小时间 500
最短距离 5

locationManager.requestLocationUpdates(provider, 500, 5,
ll);

当符合这些条件时 更新当前位置

当更新位置时 就会掉用 location的onLocationChanged方法 来更新UI

[color=red]刚开始 我封装了一个方法 updateMsg() 但是 在onLocationChanged里面调用 貌似 UI不会变化
所以 又把updateMsg()的代码写到了onLocationChanged里面[/color]

[color=blue]不过 后来 我又试了试 可以的 [/color]


[b]XML[/b]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:text=""
android:id="@+id/myTextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值