1、定位功能:
<com.amap.api.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
AMap aMap;
private Marker locationMarker;
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明定位回调监听器
public AMapLocationListener mLocationListener = new AMapLocationListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//取出经纬度
LatLng latLng = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
//添加Marker显示定位位置
if (locationMarker == null) {
//如果是空的添加一个新的,icon方法就是设置定位图标,可以自定义
locationMarker = aMap.addMarker(new MarkerOptions()
.position(latLng)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_adress)));
} else {
//已经添加过了,修改位置即可
locationMarker.setPosition(latLng);
}
//然后可以移动到定位点,使用animateCamera就有动画效果
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));
aMapLocation.getProvince();//省信息
locateCity = aMapLocation.getCity();//城市信息
mLocationClient.stopLocation();//停止定位后,本地定位服务并不会被销毁
} else {
//定位失败时,可通过ErrCode(错误码)信息来确定失败的原因,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
}
}
}
};
//初始化定位
//声明AMapLocationClientOption对象
public AMapLocationClientOption mLocationOption = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//获取权限
if (Build.VERSION.SDK_INT >= 23
&& getApplicationInfo().targetSdkVersion >= 23) {
if (isNeedCheck) {
checkPermissions(PERMISSIONS_STORAGE);
}
}
mMapView.onCreate(savedInstanceState);
initMap();
}
private void initMap() {
if (aMap == null) {
aMap = mMapView.getMap();
}
mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听
mLocationClient.setLocationListener(mLocationListener);
//初始化AMapLocationClientOption对象
mLocationOption = new AMapLocationClientOption();
//设置定位模式为AMapLocationMode.Hight_Accuracy,高精度模式。
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置定位间隔,单位毫秒,默认为2000ms,最低1000ms。
mLocationOption.setInterval(1000);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否强制刷新WIFI,默认为true,强制刷新。
mLocationOption.setWifiActiveScan(false);
//设置是否允许模拟位置,默认为true,允许模拟位置
mLocationOption.setMockEnable(false);
//单位是毫秒,默认30000毫秒,建议超时时间不要低于8000毫秒。
mLocationOption.setHttpTimeOut(20000);
//关闭缓存机制
mLocationOption.setLocationCacheEnable(false);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
2、高德地图知识点:
CameraUpdateFactory //创建CameraUpdate对象,用来改变地图形态
调用AMap.animateCamera(CameraUpdate) 或AMap.moveCamera(CameraUpdate)
static CameraUpdate newLatLng(LatLng latLng)
设置地图的中心点。
CameraUpdateFactory.newLatLng(new LatLng(36.060553, 120.38329)
static CameraUpdate zoomTo(float zoom)
设置地图缩放级别。
moveCamera(CameraUpdate update)按照传入的CameraUpdate参数改变地图状态。(直接改变状态,没有动画效果)
参数:
update - 地图状态将要发生的变化
类 LatLng:存储经纬度坐标值的类,单位角度。
LatLng(double latitude, double longitude) //latitude纬度 (垂直方向),longitude经度 (水平方向)
使用传入的经纬度构造LatLng 对象,一对经纬度值代表地球上一个地点。