实时获取GPS定位数据
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.EditText; /*
* 实时获取GPS定位数据
*/
public class LocationActivity extends Activity
{
LocationManager mLocationManager;
EditText mEditText;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main); mEditText = (EditText)findViewById(R.id.show); mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //从 GPS_PROVIDER 获取最后已知的定位信息
Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); //在editview中显示该信息
updateView(location); /*
* 请求更新 location
*/
//2000表示更新周期是2秒,8表示当新位置与当前位置超过8米时
//2000与8米是或的关系.其中一个满足时就触发LocationListener回调
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, , , new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { }
@Override
public void onProviderEnabled(String provider) {
//当PorviderLocation可用时的回调
updateView(mLocationManager.getLastKnownLocation(provider));
} @Override
public void onProviderDisabled(String provider) {
} @Override
public void onLocationChanged(Location location) {
//当gps位置信息变化时的回调
updateView(location);
}
}); } //在 EditView 中显示位置信息
public void updateView(Location newLocation)
{
if(newLocation != null)
{
StringBuilder sb = new StringBuilder(); sb.append("实时的位置信息:\n"); sb.append("经度:" + newLocation.getLongitude() + "\n");
sb.append("纬度:" + newLocation.getLatitude() + "\n");
sb.append("高度:" + newLocation.getAltitude() + "\n");
sb.append("速度:" + newLocation.getSpeed() + "\n");
sb.append("方向:" + newLocation.getBearing() + "\n"); mEditText.setText(sb.toString());
}
}
}
求两个经纬点距离
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.widget.EditText; /*
* 求两个经纬点间距离
*/
public class DistanceActivity extends Activity {
private EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_distance); mEditText = (EditText) findViewById(R.id.edittext); // 1,准备一个存放计算结果的数组,长度为3,最终距离为数组第1个元素的值,
final float[] results = new float[]; // 2,用Location.distanceBetween静态方法计算,前两个参数为第一个点的经纬度,第3,4个参数是第2个点的经纬度,
Location.distanceBetween(20.123, 30.05644, 30.124, 40.543, results);
mEditText.setText(String.valueOf(results[]) + "米");
}
}
邻近某个区域圆时警告
import mobile.android.proximity.alert.R;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle; /*
* 当进入目标区域时提示警告的示例
* 目标区域是指 以某个经纬点为原点,半径为x的一个圆.
*/
public class ProximityAlertActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //进入目标区域提示警告 第1步,得到LocationManager
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); //进入目标区域提示警告 第2步,准备目标区域
double longitude = 123.427109;
double latitude = 41.764998;
float radius = ;//半径 //进入目标区域提示警告 第3步,广播接收器的intent.PromimityAlertRecivery是个接收器.
Intent intent = new Intent(this, PromimityAlertRecivery.class); //进入目标区域提示警告 第4步,将广播接收器与PendingIntent绑定.
//PendingIntent 比intent生命周期长,应用关闭后,它仍然存在.除非卸载或关机.
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,,intent,); //进入目标区域提示警告 第5步,添加一个进入警告到LocationManager,其中-1这个参数是过期周期,-1表示永远
locationManager.addProximityAlert(latitude, longitude, radius, -, pendingIntent);
}
}
接收器的代码
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;
import android.widget.Toast; /*
* 注意这是个广播接收器,还是主动请求的数据的
*/
public class PromimityAlertRecivery { public void onReceive(Context context, Intent intent) {
//是否进入目标区域,true是进入,false是离开
boolean isEnter = intent.getBooleanExtra(
LocationManager.KEY_PROXIMITY_ENTERING, false); if (isEnter) {
Toast.makeText(context, "已经进入目标区域", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, "已经离开目标区域", Toast.LENGTH_LONG).show();
} }
}