您的位置:首页 > 其它

GPS定位例子

2016-03-15 12:33 501 查看
package com.itheima62.gpsdemo;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv_mess;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_mess = (TextView) findViewById(R.id.tv_gps_mess);
//定位 定位管理器
LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
LocationListener listener = new LocationListener() {

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub

}

/* (non-Javadoc)
* 位置变化,就触发此方法调用,覆盖此方法可以追踪回调结果信息
* @see android.location.LocationListener#onLocationChanged(android.location.Location)
*/
@Override
public void onLocationChanged(Location location) {
//获取位置变化的结果
float accuracy = location.getAccuracy();//精确度,以米为单位
double altitude = location.getAltitude();//获取海拔高度
double longitude = location.getLongitude();//获取经度
double latitude = location.getLatitude();//获取纬度
float speed = location.getSpeed();//速度

tv_mess.append("accuracy:" + accuracy + "\n");
tv_mess.append("altitude:" + altitude + "\n");
tv_mess.append("longitude:" + longitude + "\n");
tv_mess.append("latitude:" + latitude + "\n");
tv_mess.append("speed:" + speed + "\n");

}
};
/**
* provider  定位的方式: 1,wifi  2,3g/4g 消极定位  3,gps
* minTime   定位的时间差  10分钟
* minDistance 定位的距离差 10m
* listener 定位的监听回调
*/
lm.requestLocationUpdates("gps", 0, 0, listener);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

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