1.定位管理
//初始化百度地图
mBaiduMap = mMapView.getMap();
//获取百度地图定位的坐标
mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
@Override
public void onMapStatusChangeStart(MapStatus mapStatus) {
}
@Override
public void onMapStatusChangeStart(MapStatus mapStatus, int i) {
}
@Override
public void onMapStatusChange(MapStatus mapStatus) {
}
@Override
public void onMapStatusChangeFinish(MapStatus mapStatus) {
if (null != mapStatus.target) {
SPUtil.saveDataString(SPUtil.LNG, mapStatus.target.longitude + "");
SPUtil.saveDataString(SPUtil.LAT, mapStatus.target.latitude + "");
}
Logutil.d("mapStatus lat:" + mapStatus.target.latitude + ",lng:" + mapStatus.target.longitude);
}
});
//关闭地图缩放按钮
mMapView.showZoomControls(false);
//初始化定位服务
initLocationServer();
//获取地理编码
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
/**
* 初始化定位服务
*/
private void initLocationServer() {
locationService = new LocationService(this);
mListener = new JGDLocationListener();
//获取locationservice实例,建议应用中只初始化1个location实例,然后使用,可以参考其他示例的activity
// ,都是通过此种方式获取locationservice实例的
locationService.registerListener(mListener);
//注册监听
locationService.setLocationOption(locationService.getDefaultLocationClientOption());
locationService.start();// 定位SDK
Logutil.d("打开定位监听");
}
定位监听器
/**
* 定位监听
*/
private class JGDLocationListener extends BDAbstractLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
LogUtils.d("开启定位");
if (isSuccess == 2) {
int isSuccess = 2;
//设置街道信息
if (location.getLocType() == BDLocation.TypeGpsLocation) {
// GPS定位结果
isSuccess = 1;
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
// 网络定位结果
isSuccess = 1;
} else if (location.getLocType() == BDLocation.TypeOffLineLocation) {
// 离线定位结果
isSuccess = 1;
} else if (location.getLocType() == BDLocation.TypeServerError) {
isSuccess = 2;
Toastor.show("服务器错误,请检查");
} else if (location.getLocType() == BDLocation.TypeNetWorkException) {
isSuccess = 2;
Toastor.show("网络错误,请检查");
} else if (location.getLocType() == BDLocation.TypeCriteriaException) {
isSuccess = 2;
Toastor.show("手机模式错误,请检查是否飞行");
}
if (isSuccess == 1) {
LogUtils.d("定位成功");
isSuccess = 1;
//缓存当前位置的经纬度
double lat = location.getLatitude();
double lng = location.getLongitude();
mLocationLng = new LatLng(lat, lng);
Logutil.e("JGDLocationListener," + "lat:" + lat + ",lng:" + lng);
//根据定位城市获取城市对象(用于设置弹窗定位城市)
cityId = queryCityId(location.getCity());
mTvCity.setText(location.getCity() != null ? location.getCity() : "");
//地图移到到定位位置
if (isLocation) {
moveToLacation(lat, lng);
}
//关闭定位
locationService.unregisterListener(mListener);
locationService.stop();
}
}
}
}
2.地理编码
//获取地理编码
mSearch = GeoCoder.newInstance();
mSearch.setOnGetGeoCodeResultListener(this);
正反地理编码
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
if (geoCodeResult == null || geoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
//没有检索到结果
Logutil.d("onGetGeoCodeResult 没有检索到结果");
Toastor.showCentreToast("没有查找到该地址");
return;
} else {
double latitude = geoCodeResult.getLocation().latitude;
double longitude = geoCodeResult.getLocation().longitude;
latLng = new LatLng(latitude, longitude);
Logutil.e("onGetGeoCodeResult," + "lat:" + latitude + ",lng:" + longitude);
Logutil.d("当前地址:" + geoCodeResult.getAddress() + ",lat:" + geoCodeResult.getLocation().latitude + "" +
",lng:" + geoCodeResult.getLocation().longitude);
SPUtil.saveDataString(SPUtil.LAT, latitude + "");
SPUtil.saveDataString(SPUtil.LNG, longitude + "");
moveToLacation(latitude, longitude);
}
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
if (reverseGeoCodeResult == null || reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
//没有找到检索结果
Toastor.showCentreToast("没有查找到该地址");
return;
} else {
double latitude = reverseGeoCodeResult.getLocation().latitude;
double longitude = reverseGeoCodeResult.getLocation().longitude;
latLng = new LatLng(latitude, longitude);
Logutil.e("onGetReverseGeoCodeResult," + "lat:" + latitude + ",lng:" + longitude);
Logutil.d("当前地址:" + reverseGeoCodeResult.getAddress() + ",lat:" + reverseGeoCodeResult.getLocation().latitude + "" +
",lng:" + reverseGeoCodeResult.getLocation().longitude);
SPUtil.saveDataString(SPUtil.LAT, latitude + "");
SPUtil.saveDataString(SPUtil.LNG, longitude + "");
moveToLacation(latitude, longitude);
}
}
3.地图移动到相应位置
/**
* 地图移到指定坐标
*/
public void moveToLacation(double currentlat, double currentlon) {
initMapUI(new LatLng(currentlat, currentlon));
MyLocationData locData = new MyLocationData.Builder()
.accuracy(188.0f)
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(0).latitude(currentlat)
.longitude(currentlon).build();
mBaiduMap.setMyLocationData(locData);
LatLng ll = new LatLng(currentlat,
currentlon);
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(ll).zoom(19.0f);
mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
}
//中心图标定位
private void initMapUI(LatLng latLng) {
//设置当前位置Marker(圆圈)
BitmapDescriptor bitmapDescriptor =
BitmapDescriptorFactory.fromResource(R.mipmap.location_new);
OverlayOptions ooA =
new MarkerOptions().position(latLng).icon(bitmapDescriptor).yOffset
(bitmapDescriptor.getBitmap().getHeight() / 2).zIndex(9).draggable(true);
marker = (Marker) mBaiduMap.addOverlay(ooA);
MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(latLng, 19.0f);
mBaiduMap.animateMapStatus(u);
marker.setPosition(new LatLng(latLng.latitude, latLng.longitude));
}
4.地址搜索
/**
* 模糊搜索监听
*/
OnGetSuggestionResultListener listener = result -> {
if (result == null || result.getAllSuggestions() == null || result.getAllSuggestions()
.size() == 0) {
//未找到相关结果
Logutil.d("没有查找到该地址");
Toastor.showCentreToast("没有查找到该地址");
return;
} else {
List<SuggestionResult.SuggestionInfo> suggestions = result.getAllSuggestions();
if(suggestions.size()>0){
moveToLacation(suggestions.get(0).getPt().latitude,suggestions.get(0).getPt().longitude);
}else{
Toastor.showCentreToast("没有查找到该地址");
}
Logutil.d("查找到该地址:"+suggestions.size()+"个");
}
};
设置监听器
mSuggestionSearch = SuggestionSearch.newInstance();
mSuggestionSearch.setOnGetSuggestionResultListener(listener);
发起检索
mSuggestionSearch.setOnGetSuggestionResultListener(listener);
销毁
mSuggestionSearch.destroy();