Android入门:使用Android GPS实现简单的定位
来源:互联网 发布:加华伟业资本 知乎 编辑:程序博客网 时间:2024/05/22 15:22
Activity:
package com.van.gps;import android.app.Activity;import android.app.AlertDialog;import android.app.AlertDialog.Builder;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.location.Criteria;import android.location.Location;import android.location.LocationManager;import android.os.Bundle;import android.provider.Settings;import android.widget.TextView;public class GPSTestActivity extends Activity {private TextView textView;//显示文本框private LocationManager locationManager;//位置管理private GPSLocationListener locationListener;//位置监听器 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); textView = (TextView) this.findViewById(R.id.textView_location); locationListener=new GPSLocationListener(textView); //首先打开GPS,查找位置。 openGPSSettings(); } /** * 设置GPS。 */ private void openGPSSettings() { locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) { getLocation(); return; } //提示用户打开GPS AlertDialog.Builder builder = new Builder(GPSTestActivity.this); builder.setMessage("必须要开启GPS才能使用此程序,开启?"); builder.setTitle("提示"); builder.setPositiveButton("确认", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent,0); //此为设置完成后返回到获取界面 } }); builder.setNegativeButton("退出", new android.content.DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); GPSTestActivity.this.finish(); } }); builder.create().show(); } /** * 获取地理位置。 */ private void getLocation(){ // 查找到服务信息 Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度 criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(true); criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗 /** * ANDROID中有两种获取位置的方式,LocationManager.NETWORK_PROVIDER和LocationManager.GPS_PROVIDER; * 前者用于移动网络中获取位置,精度较低但速度很快, 后者使用GPS进行定位,精度很高但一般需要10-60秒时 * 间才能开始第1次定位,如果是在 室内则基本上无法定位。 * 此方法使用Criteria得到最佳的方式 */ String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息 Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置 locationListener.updateLocation(location);//调用方法,更新位置信息 // 设置监听器,1秒监听一次 locationManager.requestLocationUpdates(provider, 1000, 0 ,locationListener); }
GPSLocationListener:
package com.van.gps;import android.location.Location;import android.location.LocationListener;import android.os.Bundle;import android.widget.TextView;public class GPSLocationListener implements LocationListener{//显示文本private TextView textView;/** * 构造. * @param textView */public GPSLocationListener(TextView textView){this.textView=textView;}@Overridepublic void onLocationChanged(Location location) { updateLocation(location);}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}/** * 更新位置显示. * @param location */ public void updateLocation(Location location) { if (location != null) { double latitude = location.getLatitude(); double longitude= location.getLongitude(); textView.setText("维度:" + latitude+ "\n经度:" + longitude); } else { textView.setText("无法获取地理信息"); } } }
效果如下:
- Android入门:使用Android GPS实现简单的定位
- Android入门之简单GPS定位实例
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android GPS 定位的实现
- Android 定位GPS的使用
- Android无网络状态下使用GPS定位到省市县的一种简单实现方式
- Android GPS定位实现
- Android的GPS定位
- Android的gps定位
- Android的GPS定位
- Android GPS 定位的实现(1)
- Android GPS定位简单实例
- Android实现GPS定位功能
- Android GPS定位 一分钟快速实现GPS定位
- Android入门之GPS定位详解
- C primer plus第5章(运算符、表达式和语句)习题
- ltp 交叉编译 出现error 解决
- 趣谈JAVA常用的设计模式
- cache:缓存在asp.net中如何管理?服务器端缓存?Session, Application, Cache objectscache ,客户端缓存?Cookies,ViewState (转)
- vb学习之菜鸟方案
- Android入门:使用Android GPS实现简单的定位
- Qt中导出.txt, .xls, .xlsx, .pdf文件小结
- C#例子程序
- c#例子程序
- SQLite进阶:Android上的SQLite常用操作
- 生活场景与UML
- SQL不同服务器数据库之间的数据操作整理
- Android简单的SQLite操作及ListView展示数据
- Java虚拟机深入学习之一: Java虚拟机的内存区域