一:新建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"/>
四:效果如下图: