ITeye博客

android基于Gps 定位和基站定位获取经纬度

hack_zhang · 2013-05-20 15:10 · 地图开发 · 移动开发 · Markdown

一:新建MyLocationManager.java类,本类是为了代码架构方便把地位经纬度的代码在这类中实现然后通过回调方法,在activity中显示;

 

Java代码  收藏代码

  1. package com.android.location;
  2. import android.content.Context;
  3. import android.location.Location;
  4. import android.location.LocationListener;
  5. import android.location.LocationManager;
  6. import android.os.Bundle;
  7. public class MyLocationManager {
  8. private static Context mContext;
  9. private LocationManager gpsLocationManager;
  10. private LocationManager networkLocationManager;
  11. private static final int MINTIME = 2000;
  12. private static final int MININSTANCE = 2;
  13. private static MyLocationManager instance;
  14. private Location lastLocation = null;
  15. private static LocationCallBack mCallback;
  16. public static void init(Context c, LocationCallBack callback) {
  17. mContext = c;
  18. mCallback = callback;
  19. }
  20. private MyLocationManager() {
  21. // Gps 定位
  22. gpsLocationManager = (LocationManager) mContext
  23. .getSystemService(Context.LOCATION_SERVICE);
  24. Location gpsLocation = gpsLocationManager
  25. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  26. gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
  27. MINTIME, MININSTANCE, locationListener);
  28. // 基站定位
  29. networkLocationManager = (LocationManager) mContext
  30. .getSystemService(Context.LOCATION_SERVICE);
  31. Location networkLocation = gpsLocationManager
  32. .getLastKnownLocation(LocationManager.GPS_PROVIDER);
  33. networkLocationManager.requestLocationUpdates(
  34. LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,
  35. locationListener);
  36. }
  37. public static MyLocationManager getInstance() {
  38. if (null == instance) {
  39. instance = new MyLocationManager();
  40. }
  41. return instance;
  42. }
  43. private void updateLocation(Location location) {
  44. lastLocation = location;
  45. mCallback.onCurrentLocation(location);
  46. }
  47. private final LocationListener locationListener = new LocationListener() {
  48. public void onStatusChanged(String provider, int status, Bundle extras) {
  49. }
  50. public void onProviderEnabled(String provider) {
  51. }
  52. public void onProviderDisabled(String provider) {
  53. }
  54. public void onLocationChanged(Location location) {
  55. updateLocation(location);
  56. }
  57. };
  58. public Location getMyLocation() {
  59. return lastLocation;
  60. }
  61. private static int ENOUGH_LONG = 1000 * 60;
  62. public interface LocationCallBack {
  63. /**
  64. * 当前位置
  65. *
  66. * @param location
  67. */
  68. void onCurrentLocation(Location location);
  69. }
  70. public void destoryLocationManager() {
  71. gpsLocationManager.removeUpdates(locationListener);
  72. networkLocationManager.removeUpdates(locationListener);
  73. }
  74. }

   

 

二:在LocationActivity 中实现LocationCallBack接口,如下:

 

Java代码  收藏代码

  1. package com.android.location;
  2. import android.app.Activity;
  3. import android.location.Location;
  4. import android.os.Bundle;
  5. import android.widget.TextView;
  6. import com.android.fzmap.R;
  7. import com.android.location.MyLocationManager.LocationCallBack;
  8. public class LocationActivity extends Activity implements LocationCallBack {
  9. private TextView desText;
  10. private MyLocationManager mLocation;
  11. @Override
  12. public void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. desText = (TextView) this.findViewById(R.id.text);
  16. MyLocationManager.init(LocationActivity.this.getApplicationContext(),
  17. LocationActivity.this);//初始化
  18. mLocation = MyLocationManager.getInstance();//获取实例
  19. }
  20. //回调定位信息
  21. public void onCurrentLocation(Location location) {
  22. if (location != null) {
  23. // 显示定位结果
  24. desText.setText("当前经度:" + location.getLongitude() + "\n当前纬度:"
  25. + location.getLatitude());
  26. }
  27. }
  28. // 关闭程序也关闭定位
  29. @Override
  30. protected void onDestroy() {
  31. // TODO Auto-generated method stub
  32. super.onDestroy();
  33. mLocation.destoryLocationManager();
  34. }
  35. }

   

三:在AndroidManifest.xml中不要忘了要添加访问网络和启动定位等的几个权限

 

Xml代码  收藏代码

  1. <uses-permission android:name="android.permission.INTERNET" />
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  3. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  4. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

 

 

四:效果如下图:

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。