android 获得GPS经纬度
手机定位功能是移动手机开发者非常喜欢研究的内容,单纯的获取经纬度其实是比较简单的,上代码:
主activity MainActivity.java
package com.example.androidforgps; import android.app.Activity; import android.content.Context; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class MainActivity extends Activity { // 定义LocationManager private LocationManager locationmanager; // 定义TextView private TextView txt1; //记录 获取位置的次数 private int count; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.activity_main); // TextView 控件 txt1 = (TextView) this.findViewById(R.id.txt1); // 获取设备位置服务 locationmanager = (LocationManager) this .getSystemService(Context.LOCATION_SERVICE); // // 获取活动位置 方法一 通过GPS位置提供器获得位置(指定具体的位置提供器) // Location location = locationmanager // .getLastKnownLocation(LocationManager.GPS_PROVIDER); // 获取活动位置 方法二 使用标准集合,让系统自动选择可用的最佳位置提供器,提供位置 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度 criteria.setAltitudeRequired(false);//不需要海拔 criteria.setBearingRequired(false);//不需要方位 criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗 criteria.setCostAllowed(true);//允许有花费 //从可用的位置提供器中,匹配以上标准的最佳提供器 String provider = locationmanager.getBestProvider(criteria, true); //获得最后一次变化的位置 Location location = locationmanager.getLastKnownLocation(provider); // 打印信息 printMsg(location); //监听位置变化,2秒一次,距离0米以上 locationmanager.requestLocationUpdates(provider, 2000, 0, new LocationListener() { @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } //当位置变化时触发 @Override public void onLocationChanged(Location location) { //使用新的location更新textview显示 printMsg(location); } }); } private void printMsg(Location location) { // 获取经纬度 if (location != null) { double lat = location.getLatitude(); double lng = location.getLongitude(); // 显示信息 txt1.setText("第"+count+"次获取后:经度:" + lat + " 纬度:" + lng); } else { txt1.setText("第"+count+"次获取后:未获得信息"); } count++; } }
布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/txt1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="textview" /> </LinearLayout>
最后,切务忘记在功能清单文件中加入权限:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
主要接口分析与说明
1)、android不允许直接对LocationManager进行实例化,必须通过接口getSystemService获取LocationManager实例,如:
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);。
2)、监听器的接口onLocationChanged,主要负责捕获地理位置变化后的地理位置信息。
3)、接口requestLocationUpdates注册监听器
4)、接口getLastKnownLocation返回最后一次捕获的地理位置信息数据。
GPS测试
如果是在模拟器中调试的话,有二种方法可以进行GPS测试。
第一:通过DDMS。打开"Window->Show View->Other” 中打开"Emulator Control” View即可手动设置地理位置数据,如下图所示:
第二:使用geo命令。开始-> 运行->输入 telnet localhost 5554,然后在命令行下输入 geo fix -20.0 23.3 123,这三个参数分别代表了经度、纬度和海拔(海拔可不写)。
注:如果是使用WIN7的朋友,控制台可能会提示telnet无效什么的,那是因为WIN7下默认是不出现telnet的,需要手动打开。具体为:[1]控制面板-->程序-->打开或关闭Windows功能,然后将Telnet服务器和Telnet客户端勾选上。[2]然后在管理工具-->服务中手动启动Telnet
运行结果如下: