android GPS信息获取
在androi中GPS信息的获取可以通过系统提供的LOCATION_SERVICE中的GPS_PROVIDER获取
[java]
LocationManager GpsManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Location location = GpsManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
其中Location 中比较常用的信息有:
[java]
/*
location.getAccuracy(); 精度
location.getAltitude(); 高度 : 海拔
location.getBearing(); 导向
location.getSpeed(); 速度
location.getLatitude(); 纬度
location.getLongitude(); 经度
location.getTime(); UTC时间 以毫秒计
*/
GPS信息主要通过注册回调函数,设定限定条件让系统服务主动通知,限定条件有以下两种
1.距离: 当移动距离超过设定值时系统会主动通知 以米记
2.时间:当时间超过设定值时系统会主动通知 以豪秒记
[java]
GpsManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new GpsLocationListener());
这里设定的是1秒钟,10米为限定条件
以下给出具体的实例:
1.在布局文件中增加一个显示具体信息的文本 (activity_wifi_example.xml)
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<TextView
android:id="@+id/TextView_GpsInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</LinearLayout>
2.在代码中注册回调,设定好限制条件,实时刷新GPS状态信息 (GpsExample.java)
[java]
package com.example.gp易做图ample;
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.util.Log;
import android.view.Menu;
import android.widget.TextView;
public class GpsExample extends Activity {
private final String TAG = "GpsExample";
private TextView mGpsInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gps_example);
mGpsInfo = (TextView)this.findViewById(R.id.TextView_GpsInfo);
LocationManager GpsManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Location location = GpsManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
printGpsLocation(location);
if( location == null ) {
mGpsInfo.setText("暂无GPS有效信息");
}
GpsManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, new GpsLocationListener());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_gps_example, menu);
return true;
}
public void printGpsLocation(Location location)
{
if( location != null )
{
/*
location.getAccuracy(); 精度
location.getAltitude(); 高度 : 海拔
location.getBearing(); 导向
location.getSpeed(); 速度
location.getLatitude(); 纬度
location.getLongitude(); 经度
location.getTime(); UTC时间 以毫秒计
*/
mGpsInfo.setText("Accuracy : " + location.getAccuracy() +
"\nAltitude : " + location.getAltitude() +
"\nBearing : " + location.getBearing() +
"\nSpeed : " + location.getSpeed() +
"\nLatitude :" + location.getLatitude() +
"\nLongitude : " + location.getLongitude() +
"\nTime : " + location.getTime());
}
}
public class GpsLocationListener implements LocationListener
{
public void onLocationChanged(Location location) {
&n
补充:移动开发 , Android ,