android通过gps获取定位的位置数据和gps经纬度
package com.action.android_test;
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 location location=null;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
//获得位置服务的名称
string servicename = this.location_service;
//获得位置服务的管理对象
locationmanager locationmanager = (locationmanager)getsystemservice(servicename);
// 通过gps获取定位的位置数据
location = locationmanager.getlastknownlocation(locationmanager.gps_provider);
updatetonewlocation(location);
/**服务管理对象的监听器*/
//参数1:定位的方式 参数2:监听更新间隔时间(ms) 参数3:监听更新的距离(m) 参数4:监听的方法
locationmanager.requestlocationupdates(locationmanager.gps_provider, 2000, 10, new locationlistener() {
public void onstatuschanged(string provider, int status, bundle extras) {
}
public void onproviderenabled(string provider) {
}
public void onproviderdisabled(string provider) {
}
public void onlocationchanged(location location) {
updatetonewlocation(location);
}
});
}
private void updatetonewlocation(location location) {
textview tv1;
tv1 = (textview) this.findviewbyid(r.id.tv1);
if (location != null) {
double latitude = location.getlatitude();
double longitude= location.getlongitude();
tv1.settext("经纬度为:n"+"维度:" + latitude+ "n经度" + longitude);
} else {
tv1.settext("无法获取地理信息");
}
}
@override
public boolean oncreateoptionsmenu(menu menu) {
getmenuinflater().inflate(r.menu.main, menu);
return true;
}
上一篇: [PHP] 现代化PHP之路:composer的安装和升级
下一篇: 您知道血糖高吃什么好吗