获取开发这密钥以及下载相关的jar包百度开发者文档有详细的说明,不多说,直接上代码。
1.
SDKInitializer.initialize(getApplicationContext());
不出意外此时地图已经显示出来了。
2.定位:
mBaiduMap.setMyLocationEnabled(true);
LocationClient mLocationClient = new LocationClient(this);
LocationClientOption option = new LocationClientOption();
option.setIsNeedAddress(true);
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//设置高精度定位定位模式
option.setOpenGps(true); // 打开gps
option.setCoorType("bd09ll"); // 设置坐标类型
option.setScanSpan(1000);
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
mBaiduMap.setMyLocationEnabled(true);
locations = location;
// 构造定位数据
MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-360
.direction(0).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
// 设置定位数据
mBaiduMap.setMyLocationData(locData);
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
BitmapDescriptor mCurrentMarker = BitmapDescriptorFactory
.fromResource(R.mipmap.ic_launcher);
MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.FOLLOWING, true, null);
mBaiduMap.setMyLocationConfiguration(config);
// 当不需要定位图层时关闭定位图层
}
});
mLocationClient.start();
3.poi检索功能分三种,城市检索,周边检索,矩形区域检索
首先注册一个检索监听者,在回掉中标记marker点,灰色背景的代码即为设置marker点的方法,同时将检索位置的信息通过bundle传入marker信息中,方便对marker的后续操作。
PoiSearch mPoiSearch = PoiSearch.newInstance();
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public void onGetPoiResult(PoiResult result) {
//获取POI检索结果
if (result.getAllPoi() != null) {
mData = result.getAllPoi();
//定义Maker坐标点
for (PoiInfo p : mData) {
LatLng point = new LatLng(p.location.latitude, p.location.longitude);
//构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.mipmap.ic_launcher);
//构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions()
.position(point)
.icon(bitmap);
Bundle bundle = new Bundle();
//info必须实现序列化接口
bundle.putParcelable("info", p);
mBaiduMap.addOverlay(option).setExtraInfo(bundle);
//在地图上添加Marker,并显示
}
}
}
public void onGetPoiDetailResult(PoiDetailResult result) {
//获取Place详情页检索结果
Log.e("result", result.toString());
}
@Override
public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {
}
};
mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
周边检索:
mPoiSearch.searchNearby((new PoiNearbySearchOption().pageCapacity(50)).radius(10000)
.location(new LatLng(locations.getLatitude(),locations.getLongitude()))
.keyword(et.getText().toString()));
mPoiSearch.searchInCity((new PoiCitySearchOption().pageCapacity(50))
.city("烟台")
.keyword(et.getText().toString()));
LatLng southwest = new LatLng(locations.getLatitude() - 0.01, locations.getLongitude() - 0.012);// 西南
LatLng northeast = new LatLng(locations.getLatitude() + 0.01, locations.getLongitude() + 0.012);// 东北
mPoiSearch.searchInBound(new PoiBoundSearchOption().bound(new LatLngBounds.Builder().include(southwest).include(northeast).build()).keyword("酒店"));
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
//从marker中获取info信息
Bundle bundle = marker.getExtraInfo();
PoiInfo infoUtil = (PoiInfo) bundle.getParcelable("info");
// mSpeechSynthesizer.speak(infoUtil.name);
ImageView tv = new ImageView(MapActivity.this);
BitmapDescriptor bitmapDescriptor;
bitmapDescriptor = BitmapDescriptorFactory.fromView(tv);
//infowindow位置
LatLng latLng = new LatLng(marker.getPosition().latitude, marker.getPosition().longitude);
//infowindow点击事件
InfoWindow.OnInfoWindowClickListener listener = new InfoWindow.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick() {
//隐藏infowindow
mBaiduMap.hideInfoWindow();
}
};
//显示infowindow,-47是偏移量,使infowindow向上偏移,不会挡住marker
InfoWindow infoWindow = new InfoWindow(bitmapDescriptor, latLng, -107, listener);
mBaiduMap.showInfoWindow(infoWindow);
return true;
}
});