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

 

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

package com.android.location;

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接口,如下:

package com.android.location;

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;

	@Override
	public 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());
		}
	}

	// 关闭程序也关闭定位
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.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"/>
 

 

四:效果如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值