读书人

android基于Gps 定位跟基站定位获取经

发布时间: 2012-09-24 13:49:41 作者: rapoo

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

?

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

?

import android.content.Context;import android.location.Location;import android.location.LocationListener;import android.location.LocationManager;import android.os.Bundle;public class MyLocationManager {private static Context mContext;private LocationManager gpsLocationManager;private LocationManager networkLocationManager;private static final int MINTIME = 2000;private static final int MININSTANCE = 2;private static MyLocationManager instance;private Location lastLocation = null;private static LocationCallBack mCallback;public static void init(Context c, LocationCallBack callback) {mContext = c;mCallback = callback;}private MyLocationManager() {// Gps 定位gpsLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);Location gpsLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);gpsLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINTIME, MININSTANCE, locationListener);// 基站定位networkLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);Location networkLocation = gpsLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);networkLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MINTIME, MININSTANCE,locationListener);}//单例模式实例化类public static MyLocationManager getInstance() {if (null == instance) {instance = new MyLocationManager();}return instance;}private void updateLocation(Location location) {lastLocation = location;mCallback.onCurrentLocation(location);}private final LocationListener locationListener = new LocationListener() {public void onStatusChanged(String provider, int status, Bundle extras) {}public void onProviderEnabled(String provider) {}public void onProviderDisabled(String provider) {}public void onLocationChanged(Location location) {updateLocation(location);}};public Location getMyLocation() {return lastLocation;}private static int ENOUGH_LONG = 1000 * 60;public interface LocationCallBack {/** * 当前位置 *  * @param location */void onCurrentLocation(Location location);}//注销注册监听public void destoryLocationManager() {gpsLocationManager.removeUpdates(locationListener);networkLocationManager.removeUpdates(locationListener);}}
?

?

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

?

import android.app.Activity;import android.location.Location;import android.os.Bundle;import android.widget.TextView;import com.android.fzmap.R;import com.android.location.MyLocationManager.LocationCallBack;public class LocationActivity extends Activity implements LocationCallBack {private TextView desText;private MyLocationManager mLocation;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);desText = (TextView) this.findViewById(R.id.text);MyLocationManager.init(LocationActivity.this.getApplicationContext(),LocationActivity.this);//初始化mLocation = MyLocationManager.getInstance();//获取实例}//回调定位信息public void onCurrentLocation(Location location) {if (location != null) {// 显示定位结果desText.setText("当前经度:" + location.getLongitude() + "\n当前纬度:"+ location.getLatitude());}}// 关闭程序也关闭定位@Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();mLocation.destoryLocationManager();}}
?

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

<uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
?

?

四:效果如下图:


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

读书人网 >Android

热点推荐