package xhs.getlocation;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
LocationManager locationManager;
private EditText editText;
Button btnGetLocation;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnGetLocation=(Button)findViewById(R.id.btnGetLocation);
editText = (EditText)findViewById(R.id.speed);
editText.setText("Location:\n");
btnGetLocation.setOnClickListener(new bntOnClickListen());
//通过getSystemService接口获取LocationManager实例
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
//实现监听器 LocationListener
LocationListener locationlisten=new LocationListener() {
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
}
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
// Provider被enable时触发此函数,比如GPS被打开
}
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
// Provider被disable时触发此函数,比如GPS被关闭
}
//当坐标改变时触发此函数;如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
if (arg0 != null) {
Log.i("log", "Location changed : Lat: " + arg0.getLatitude() + " Lng: " + arg0.getLongitude());
editText.setText("Lat:"+arg0.getLatitude() + " \nLng: " + arg0.getLongitude() + "\n");
}
else {
Log.i("log", "Location changed : Lat: " + "NULL" + " Lng: " + "NULL");
editText.setText("NULL" + " Lng: " + "NULL" + "\n");
}
}
};
// 注册监听器 locationListener
//第 2 、 3个参数可以控制接收GPS消息的频度以节省电力。第 2个参数为毫秒, 表示调用 listener的周期,第 3个参数为米 ,表示位置移动指定距离后就调用 listener
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationlisten);
}
class bntOnClickListen implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取地理位置信息数据(如果没实现监听器和注册监听器,好像获取不了地理数据)
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null) {
String strLati = Double.toString(location.getLatitude()); //经度
String strLong = Double.toString(location.getLongitude()); //纬度
//显示地理位置数据
System.out.println("---------地理位置信息---------");
System.out.println("---------" + strLati + "/" + strLong + "---------");
editText.setText(strLati + "/" + strLong + "\n");
}
else{
Log.i("log", "location==NULL");
editText.setText("location==NULL");
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
android GPS.rar (66个子文件)
Service
bin
classes.dex 10KB
com
xhs
service
MainActivity.class 1KB
MainActivity$1.class 1KB
ExampleService.class 1KB
R$layout.class 452B
R$id.class 601B
R$drawable.class 391B
IntentActivity$1.class 2KB
BinderService$MyBinder.class 605B
BinderService.class 2KB
BinderActivity$1$1.class 2KB
BinderActivity.class 2KB
R$attr.class 331B
R.class 510B
BinderActivity$1.class 2KB
IntentActivity.class 1KB
R$string.class 418B
ExampleIntentService.class 1KB
MyService$MyThread.class 1KB
MyService.class 1KB
resources.ap_ 12KB
Service.apk 19KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 174B
drawable-mdpi
icon.png 3KB
layout
intentservice.xml 568B
binder.xml 555B
main.xml 542B
assets
default.properties 362B
gen
com
xhs
service
R.java 1KB
proguard.cfg 1KB
src
com
xhs
service
BinderService.java 1KB
MyService.java 1004B
ExampleIntentService.java 611B
BinderActivity.java 2KB
ExampleService.java 924B
MainActivity.java 1KB
IntentActivity.java 1KB
.project 843B
.classpath 280B
AndroidManifest.xml 1KB
GPSLocation
bin
classes.dex 5KB
xhs
getlocation
MainActivity.class 2KB
MainActivity$1.class 2KB
R$layout.class 385B
R$id.class 444B
R$drawable.class 391B
R$attr.class 331B
R.class 510B
R$string.class 418B
MainActivity$bntOnClickListen.class 2KB
GPSLocation.apk 15KB
resources.ap_ 11KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 174B
drawable-mdpi
icon.png 3KB
layout
main.xml 675B
assets
default.properties 362B
gen
xhs
getlocation
R.java 845B
proguard.cfg 1KB
src
xhs
getlocation
MainActivity.java 4KB
.project 847B
.classpath 280B
AndroidManifest.xml 766B
共 66 条
- 1
资源评论
- u0102736252014-04-15不能用 是因为在室内吗?
- stony20152013-01-29例子很不错,对我很有用
- kkklko2013-06-28不错哈,不过在室内不能用。。
- kakafabregas2012-07-13例子很不错!但是我对服务的那部分理解不是很好!
无谓生活
- 粉丝: 42
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 自动驾驶定位系列教程十:闭环修正.pdf
- HM2333-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- Python实现插入排序算法(源代码)
- 123.cpp
- HM2319-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- modbus4j-3.0.4.jar
- 蒙特·卡罗实验、使用蒙特·卡罗方法计算圆周率近似值.docx
- HM2319A-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- JAVA SpringBoot 集成华为云OBS,多镜像配置settings
- 一个文件共享系统,包括前端文件展示系统和后台管理系统,基于SpringBoot + MyBatis实现
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功