通过GPS取得的是一个Location类型的经纬度, 可以转换为两个Double 纬度和经度.

纬度: 23.223871812820435

纬度: 113.58986039161628

首先创建一个TextView和两个Button

mian.xml

    <TextView  android:id="@+id/text"   android:layout_width="fill_parent"     android:layout_height="wrap_content"     />        <Button    android:id="@+id/btnStart"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="定位"/>    <Button    android:id="@+id/btnStop"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="停止"/>

然后添加主Activity的代码

Location 是存放经纬度的一个类型

LocationManager是位置管理服务类型

private Button btnStart;private Button btnStop;private TextView textView;private Location mLocation; private LocationManager mLocationManager;   /** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);btnStart = (Button)findViewById(R.id.btnStart);btnStop  = (Button)findViewById(R.id.btnStop);textView = (TextView)findViewById(R.id.text);btnStart.setOnClickListener(btnClickListener);//开始定位btnStop.setOnClickListener(btnClickListener);//结束定位按钮}

gpsIsOpen是自己写的查看当前GPS是否开启
getLocation 是自己写的一个获取定位信息的方法

mLocationManager.removeUpdates()是停止当前的GPS位置监听

public Button.OnClickListener btnClickListener = new Button.OnClickListener(){public void onClick(View v){Button btn = (Button)v;if(btn.getId() == R.id.btnStart){if(!gpsIsOpen())return;  mLocation = getLocation();if(mLocation != null)textView.setText("维度:" + mLocation.getLatitude() + "\n经度:" + mLocation.getLongitude());else textView.setText("获取不到数据");}else if(btn.getId() == R.id.btnStop){mLocationManager.removeUpdates(locationListener);}}};

private boolean gpsIsOpen(){boolean bRet = true;LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER)){Toast.makeText(this, "未开启GPS", Toast.LENGTH_SHORT).show();bRet = false;}else {Toast.makeText(this, "GPS已开启", Toast.LENGTH_SHORT).show();}return bRet;}


判断当前是否开启GPS

private boolean gpsIsOpen(){boolean bRet = true;LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER)){Toast.makeText(this, "未开启GPS", Toast.LENGTH_SHORT).show();bRet = false;}else {Toast.makeText(this, "GPS已开启", Toast.LENGTH_SHORT).show();}return bRet;}

该方法获取当前的经纬度, 第一次获取总是null

后面从LocationListener获取已改变的位置

mLocationManager.requestLocationUpdates()是开启一个LocationListener等待位置变化

private Location getLocation(){//获取位置管理服务mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);//查找服务信息Criteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高criteria.setAltitudeRequired(false);//海拔信息:不需要criteria.setBearingRequired(false);//方位信息: 不需要criteria.setCostAllowed(true);//是否允许付费criteria.setPowerRequirement(Criteria.POWER_LOW);  //耗电量: 低功耗String provider = mLocationManager.getBestProvider(criteria, true); //获取GPS信息Location location = mLocationManager.getLastKnownLocation(provider);mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener);return location;}


改方法是等待GPS位置改变后得到新的经纬度

private final LocationListener locationListener = new LocationListener(){public void onLocationChanged(Location location){// TODO Auto-generated method stubif(location != null)textView.setText("维度:" + location.getLatitude() + "\n经度:" + location.getLongitude());else textView.setText("获取不到数据" + Integer.toString(nCount));}public void onProviderDisabled(String provider){// TODO Auto-generated method stub}public void onProviderEnabled(String provider){// TODO Auto-generated method stub}public void onStatusChanged(String provider, int status, Bundle extras){// TODO Auto-generated method stub}};


更多相关文章

  1. 获取Android手机上的图片和视频缩略图
  2. 下载 android source 之repo获取
  3. android:Bitmap和Drawable相互转换方法
  4. Android(安卓)获取手机的厂商、型号、Android系统版本号、IMEI、
  5. Android通过获取Ip的方法判断手机是否联网
  6. 获取Android(安卓)ics源码
  7. 工具类之FragmentUtils
  8. 地图API使用文档-以腾讯地图为例
  9. Android实现QQ图片说说照片选择效果

随机推荐

  1. 移动Web App开发之实战美团外卖
  2. 初识C语言2
  3. Python主讲移动端自动化测试框架Appium
  4. 【大云制造】为云而生 - 大云BEK内核
  5. 【干货分享】Kubernetes容器网络之CNI漫
  6. 【干货分享】Linux操作系统自动化测试平
  7. 知道 Redis-Cluster 么?说说其中可能不可
  8. 【干货分享】硬件加速介绍及Cyborg项目代
  9. PHP数组常用函数
  10. 【干货分享】虚拟机热迁移性能优化