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

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

 

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

 

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

 

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

三: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"/>  
 

 

四:效果如下图:


猜你喜欢

转载自hack-zhang.iteye.com/blog/1872149