首页 > 系统 > Android > 正文

Android实现GPS定位代码实例

2019-10-24 20:36:29
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Android实现GPS定位实例,对关键操作部份给出代码示例并做了一定的注释,需要的朋友可以参考下

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

纬度: 23.223871812820435

纬度: 113.58986039161628

首先创建一个TextView和两个Button

 

 
  1. <TextView 
  2. android:id="@+id/text" 
  3. android:layout_width="fill_parent" 
  4. android:layout_height="wrap_content" /> 
  5.  
  6. <Button 
  7. android:id="@+id/btnStart" 
  8. android:layout_width="wrap_content" 
  9. android:layout_height="wrap_content" 
  10. android:text="定位" /> 
  11. <Button 
  12. android:id="@+id/btnStop" 
  13. android:layout_width="wrap_content" 
  14. android:layout_height="wrap_content" 
  15. android:text="停止" /> 

然后添加主Activity的代码

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

LocationManager是位置管理服务类型

 

 
  1. private Button btnStart; 
  2. private Button btnStop; 
  3. private TextView textView; 
  4. private Location mLocation; 
  5. private LocationManager mLocationManager; 
  6. /** Called when the activity is first created. */ 
  7. @Override 
  8. public void onCreate(Bundle savedInstanceState) 
  9.  
  10. super.onCreate(savedInstanceState); 
  11. setContentView(R.layout.main); 
  12.  
  13. btnStart = (Button)findViewById(R.id.btnStart); 
  14. btnStop = (Button)findViewById(R.id.btnStop); 
  15. textView = (TextView)findViewById(R.id.text); 
  16. btnStart.setOnClickListener(btnClickListener); //开始定位 
  17. btnStop.setOnClickListener(btnClickListener); //结束定位按钮 
  18. gpsIsOpen是自己写的查看当前GPS是否开启 
  19. getLocation 是自己写的一个获取定位信息的方法 
  20. mLocationManager.removeUpdates()是停止当前的GPS位置监听 
  21. public Button.OnClickListener btnClickListener = new Button.OnClickListener() 
  22. public void onClick(View v) 
  23. Button btn = (Button)v; 
  24. if(btn.getId() == R.id.btnStart) 
  25. if(!gpsIsOpen()) 
  26. return
  27.  
  28. mLocation = getLocation(); 
  29.  
  30. if(mLocation != null
  31. textView.setText("维度:" + mLocation.getLatitude() + "/n经度:" + mLocation.getLongitude()); 
  32. else 
  33. textView.setText("获取不到数据"); 
  34. else if(btn.getId() == R.id.btnStop) 
  35. mLocationManager.removeUpdates(locationListener); 
  36.  
  37. }; 
  38. private boolean gpsIsOpen() 
  39. boolean bRet = true
  40.  
  41. LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
  42. if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
  43. Toast.makeText(this"未开启GPS", Toast.LENGTH_SHORT).show(); 
  44. bRet = false
  45. else 
  46. Toast.makeText(this"GPS已开启", Toast.LENGTH_SHORT).show(); 
  47.  
  48. return bRet; 
  49. 判断当前是否开启GPS 
  50. private boolean gpsIsOpen() 
  51. boolean bRet = true
  52.  
  53. LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
  54. if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER)) 
  55. Toast.makeText(this"未开启GPS", Toast.LENGTH_SHORT).show(); 
  56. bRet = false
  57. else 
  58. Toast.makeText(this"GPS已开启", Toast.LENGTH_SHORT).show(); 
  59.  
  60. return bRet; 
  61. 该方法获取当前的经纬度, 第一次获取总是null 
  62. 后面从LocationListener获取已改变的位置 
  63. mLocationManager.requestLocationUpdates()是开启一个LocationListener等待位置变化 
  64. private Location getLocation() 
  65. //获取位置管理服务 
  66. mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
  67.  
  68. //查找服务信息 
  69. Criteria criteria = new Criteria(); 
  70. criteria.setAccuracy(Criteria.ACCURACY_FINE); //定位精度: 最高 
  71. criteria.setAltitudeRequired(false); //海拔信息:不需要 
  72. criteria.setBearingRequired(false); //方位信息: 不需要 
  73. criteria.setCostAllowed(true); //是否允许付费 
  74. criteria.setPowerRequirement(Criteria.POWER_LOW); //耗电量: 低功耗 
  75.  
  76. String provider = mLocationManager.getBestProvider(criteria, true); //获取GPS信息 
  77.  
  78. Location location = mLocationManager.getLastKnownLocation(provider); 
  79.  
  80. mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener); 
  81.  
  82. return location; 
  83. 改方法是等待GPS位置改变后得到新的经纬度 
  84. private final LocationListener locationListener = new LocationListener() 
  85. public void onLocationChanged(Location location) 
  86. // TODO Auto-generated method stub 
  87. if(location != null
  88. textView.setText("维度:" + location.getLatitude() + "/n经度:" 
  89. + location.getLongitude()); 
  90. else 
  91. textView.setText("获取不到数据" + Integer.toString(nCount)); 
  92.  
  93. public void onProviderDisabled(String provider) 
  94. // TODO Auto-generated method stub 
  95.  
  96. public void onProviderEnabled(String provider) 
  97. // TODO Auto-generated method stub 
  98.  
  99. public void onStatusChanged(String provider, int status, Bundle extras) 
  100. // TODO Auto-generated method stub 
  101.  
  102. }; 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表