import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
/**
* 多种定位
*
*/
public class ManyLocation implements LocationSource, AMapLocationListener {
private OnLocationChangedListener mListener;
private LocationManagerProxy mAMapLocationManager;
Context mContext;
AMap mAMap;
/**
* 构造方法
*
* @param context
* @param aMap
*/
public ManyLocation(Context context, AMap aMap) {
this.mContext = context;
this.mAMap = aMap;
}
/**
* 设置amap的locationType
*
* 设置定位的类型为定位模式 AMap.LOCATION_TYPE_LOCATE 设置定位的类型为跟随模式
* AMap.LOCATION_TYPE_MAP_FOLLOW 设置定位的类型为根据地图面向方向旋转
* AMap.LOCATION_TYPE_MAP_ROTATE
*
* @param locationType
*/
public void setUpMap(int locationType) {
// 设置定位监听
mAMap.setLocationSource(this);
// 设置默认定位按钮是否显示
mAMap.getUiSettings().setMyLocationButtonEnabled(true);
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
mAMap.setMyLocationEnabled(true);
// 设置定位的类型为定位模式 ,可以由定位、跟随或地图根据面向方向旋转几种
mAMap.setMyLocationType(locationType);
}
/**
* 此方法已经废弃
*/
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
/**
* 定位成功后回调函数
*/
@Override
public void onLocationChanged(AMapLocation aLocation) {
if (mListener != null && aLocation != null) {
// 显示系统小蓝点
mListener.onLocationChanged(aLocation);
}
}
/**
* 激活定位
*/
@SuppressWarnings("deprecation")
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
if (mAMapLocationManager == null) {
mAMapLocationManager = LocationManagerProxy.getInstance(mContext);
/*
* mAMapLocManager.setGpsEnable(false);
* 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location
* API定位采用GPS和网络混合定位方式
* ,第一个参数是定位provider,第二个参数时间最短是2000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
*/
mAMapLocationManager.requestLocationUpdates(
LocationProviderProxy.AMapNetwork, 2000, 10, this);
}
}
/**
* 停止定位
*/
@SuppressWarnings("deprecation")
@Override
public void deactivate() {
mListener = null;
if (mAMapLocationManager != null) {
mAMapLocationManager.removeUpdates(this);
mAMapLocationManager.destory();
}
mAMapLocationManager = null;
}
}
转载于:https://my.oschina.net/spoon2014/blog/420554