```
/**
* Created by chenmeng on 2016/9/2.
*/
public class BaiduMapUtil {
private final static String TAG = BaiduMapUtil.class.getSimpleName();
private Context mContext;
public BaiduMap mBaiduMap;
private Marker marker;
private PoiSearch mPoiSearch;/// 兴趣点检索
private RoutePlanSearch routeSearch;//公交检索
private int routeSearchType;
public WalkingRouteLine mWalkingRouteLine;
public TransitRouteLine mTransitRouteLine;
public DrivingRouteLine mDrivingRouteLine;
private TransitRouteOverlay mTransitRouteOverlay;
private WalkingRouteOverlay mWalkingRouteOverlay;
private DrivingRouteOverlay mDrivingRouteOverlay;
private int nodeIndex = -2;//公交站点索引
public static final int SEARCH_TYPE_CITY = 0;
public static final int SEARCH_TYPE_BOUND = 1;
public static final int SEARCH_TYPE_NEARBY = 2;
public static final int TYPE_WALK = 0;
public static final int TYPE_BUS = 1;
public static final int TYPE_DRIVE = 2;
private GeoCoder geoCoder;
public Geocallback geocallback;
private Navigation navigation;
public BaiduMapUtil(Context context){
this.mContext=context;
this.geoCoder=GeoCoder.newInstance();
}
public BaiduMapUtil(Context context, MapView mapView) {
this.mContext = context;
SDKInitializer.initialize(mContext);
this.mBaiduMap = mapView.getMap();
mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Bundle bundle=marker.getExtraInfo();
BaseLocation baseLocation= (BaseLocation) bundle.getSerializable("info");
showInfoWindow(baseLocation);
return true;
}
});
this.mPoiSearch=PoiSearch.newInstance();
this.routeSearch=RoutePlanSearch.newInstance();
this.geoCoder=GeoCoder.newInstance();
}
//将地图显示到此处
public void showAtLocation(BaseLocation location) {
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(latLng);
mBaiduMap.animateMapStatus(u);
}
//添加图层
public void addOverLay(BaseLocation baseLocation) {
BitmapDescriptor mIconMaker=null;
LatLng latLng;
OverlayOptions overlayOptions = null;
latLng = new LatLng(baseLocation.getLatitude(), baseLocation.getLongitude());
if(baseLocation instanceof LocationInfo) {
mIconMaker = BitmapDescriptorFactory.fromResource(R.mipmap.icon_geo);
}
if(baseLocation instanceof HospitalInfo){
mIconMaker = BitmapDescriptorFactory.fromResource(R.mipmap.icon_gcoding);
}
overlayOptions = new MarkerOptions().position(latLng)
.icon(mIconMaker).zIndex(2);
marker = (Marker) (mBaiduMap.addOverlay(overlayOptions));
Bundle bundle = new Bundle();
bundle.putSerializable(“info”, baseLocation);
marker.setExtraInfo(bundle);
}
//折线图
public void addPolyline(List list){
OverlayOptions overlayOptions=new PolylineOptions()
.points(list)
.width(5)
.color(Color.BLUE);
mBaiduMap.addOverlay(overlayOptions);
}
//位置信息框
public void showInfoWindow(final BaseLocation baseLocation) {
Log.d(TAG, baseLocation.getLatitude() + “”);
LayoutInflater layoutInflater;
layoutInflater = LayoutInflater.from(mContext);
View view = null;
InfoWindow mInfoWindow;
ViewHolder viewHolder;
ViewHolderHos viewHolderHos;
if (baseLocation instanceof LocationInfo) {
view = layoutInflater.inflate(R.layout.trip_popupwindow, null);
if (view.getTag() == null) {
viewHolder = new ViewHolder();
viewHolder.txDate = (TextView) view.findViewById(R.id.tx_date);
viewHolder.txTime = (TextView) view.findViewById(R.id.tx_time);
viewHolder.txAddress = (TextView) view.findViewById(R.id.tx_address);
view.setTag(viewHolder);
}
viewHolder = (ViewHolder) view.getTag();
viewHolder.txDate.setText(((LocationInfo) baseLocation).getTime());
viewHolder.txAddress.setText(((LocationInfo) baseLocation).getAddress());
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBaiduMap.hideInfoWindow();
}
});
}
if(baseLocation instanceof HospitalInfo){
view =layoutInflater.inflate(R.layout.hospital_popupwindow, null);
if(view.getTag()==null){
viewHolderHos=new ViewHolderHos();
viewHolderHos.linearWindowInfo=(LinearLayout)view.findViewById(R.id.linear_windowinfo);
viewHolderHos.linearWindowInfo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(“linearWindowInfo”, “linearWindowInfo”);
mBaiduMap.hideInfoWindow();
}
});
viewHolderHos.txName=(TextView)view.findViewById(R.id.tx_hospitalname);
viewHolderHos.txAddress=(TextView)view.findViewById(R.id.tx_hospitaladdress);
viewHolderHos.txNavigation=(TextView)view.findViewById(R.id.tx_hospitalnavigation);
viewHolderHos.txNavigation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("txNavigation","txNavigation");
navigation.navigation((HospitalInfo) baseLocation);
}
});
view.setTag(viewHolderHos);
}
viewHolderHos=(ViewHolderHos)view.getTag();
viewHolderHos.txName.setText(((HospitalInfo) baseLocation).getPoiname());
viewHolderHos.txAddress.setText(((HospitalInfo) baseLocation).getPoiadd());
}
mInfoWindow = new InfoWindow(view, new LatLng(baseLocation.getLatitude(), baseLocation.getLongitude()),-47);
mBaiduMap.showInfoWindow(mInfoWindow);
}
private class ViewHolder {
TextView txDate;
TextView txTime;
TextView txAddress;
}
private class ViewHolderHos{
LinearLayout linearWindowInfo;
TextView txName;
TextView txAddress;
TextView txNavigation;
}
//发起反地理编码
public void GeoCode(BaseLocation baseLocation) {
LatLng latLng = new LatLng(baseLocation.getLatitude(), baseLocation.getLongitude());
this.geoCoder.setOnGetGeoCodeResultListener(geoListener);
this.geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(latLng));
}
OnGetGeoCoderResultListener geoListener = new OnGetGeoCoderResultListener() {
@Override
public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
}
@Override
public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
if (reverseGeoCodeResult == null
|| reverseGeoCodeResult.error != SearchResult.ERRORNO.NO_ERROR) {
// 没有检测到结果
ToastShow.LongToast(mContext, "查询失败");
}
String address = reverseGeoCodeResult.getAddress();
LatLng latLng = reverseGeoCodeResult.getLocation();
Log.d("chenmeng1", address);
geocallback.geo(address);
}
};
public interface Geocallback{
void geo(String address);
void Poi(List<HospitalInfo> list);
}
public void setGeoCallback(Geocallback geoCallback){
this.geocallback=geoCallback;
}
public void searchByType(int type, LatLng mLatLng) {
mPoiSearch.setOnGetPoiSearchResultListener(poiListener);
switch (type) {
case SEARCH_TYPE_CITY:
PoiCitySearchOption option = new PoiCitySearchOption();
option.city("西安");
option.keyword("医院");
option.pageCapacity(10);
option.pageNum(10);
mPoiSearch.searchInCity(option);
break;
case SEARCH_TYPE_BOUND:
PoiBoundSearchOption boundSearchOption = new PoiBoundSearchOption();
boundSearchOption.keyword("医院");
boundSearchOption.pageCapacity(10);
boundSearchOption.pageNum(10);
//设置搜索范围
LatLng southwest = new LatLng(mLatLng.latitude - 0.01, mLatLng.longitude - 0.012);// 西南
LatLng northeast = new LatLng(mLatLng.latitude + 0.01, mLatLng.latitude + 0.012);// 东北
LatLngBounds bounds = new LatLngBounds.Builder().include(southwest)
.include(northeast).build();// 得到一个地理范围对象
boundSearchOption.bound(bounds);
mPoiSearch.searchInBound(boundSearchOption);
break;
case SEARCH_TYPE_NEARBY:
PoiNearbySearchOption nearbySearchOption = new PoiNearbySearchOption();
nearbySearchOption.location(mLatLng);
nearbySearchOption.pageNum(10);
nearbySearchOption.pageCapacity(10);
nearbySearchOption.keyword("医院");
nearbySearchOption.radius(2000);
nearbySearchOption.sortType(PoiSortType.distance_from_near_to_far);
mPoiSearch.searchNearby(nearbySearchOption);
break;
default:
break;
}
}
/**
* 创建POI检索监听器
*/
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
@Override
public void onGetPoiResult(PoiResult poiResult) {
ArrayList<HospitalInfo> arrayList = new ArrayList<>();
ArrayList<PoiInfo> mList;
Log.d("chenmeng1",poiResult.getAllPoi().size()+"");
mList = (ArrayList) poiResult.getAllPoi();
if (mList != null) {
for (int i = 0; i < mList.size(); i++) {
String poiname = mList.get(i).name;
String poiadd = mList.get(i).address;
String idString = mList.get(i).uid;
String phoneNum = mList.get(i).phoneNum;
LatLng latLng = mList.get(i).location;
double longitude = latLng.longitude;
double latitude = latLng.latitude;
HospitalInfo hospitalInfo = new HospitalInfo(poiname, poiadd, phoneNum, latitude,longitude);
arrayList.add(hospitalInfo);
Log.d("chenmeng1","arrayList.size()="+arrayList.size());
}
geocallback.Poi(arrayList);
}
}
@Override
public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {
}
};
public void routeSearch(LatLng startLatLng,LatLng endLatLng,int type) {
routeSearch = RoutePlanSearch.newInstance();
//第三步,设置公交线路规划检索监听者;
routeSearch.setOnGetRoutePlanResultListener(routePlanResultListener);
PlanNode stNode = PlanNode.withLocation(startLatLng);
PlanNode enNode = PlanNode.withLocation(endLatLng);
nodeIndex = -1;
switch (type) {
case TYPE_WALK:
routeSearch.walkingSearch(new WalkingRoutePlanOption()
.from(stNode)
.to(enNode));
routeSearchType = 0;
break;
case TYPE_BUS:
routeSearch.transitSearch(new TransitRoutePlanOption()
.from(stNode)
.city("西安")
.to(enNode));
routeSearchType = 1;
break;
case TYPE_DRIVE:
routeSearch.drivingSearch(new DrivingRoutePlanOption()
.from(stNode)
.to(enNode));
routeSearchType = 2;
break;
default:
break;
}
}
OnGetRoutePlanResultListener routePlanResultListener = new OnGetRoutePlanResultListener() {
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(mContext, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mWalkingRouteOverlay = new WalkingRouteOverlay(mBaiduMap);
mBaiduMap.setOnMarkerClickListener(mWalkingRouteOverlay);
mBaiduMap.clear();
// 此处仅展示一个方案作为示例
mWalkingRouteOverlay.setData(result.getRouteLines().get(0));
mWalkingRouteOverlay.addToMap();
mWalkingRouteOverlay.zoomToSpan();
// 将路线数据保存给全局变量
mWalkingRouteLine = result.getRouteLines().get(0);
// 重置路线节点索引,节点浏览时使用
nodeIndex = -1;
}
}
@Override
public void onGetTransitRouteResult(TransitRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(mContext, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mTransitRouteOverlay = new TransitRouteOverlay(mBaiduMap);
mBaiduMap.clear();
mBaiduMap.setOnMarkerClickListener(mTransitRouteOverlay);
mTransitRouteOverlay.setData(result.getRouteLines().get(0));
mTransitRouteOverlay.addToMap();
mTransitRouteOverlay.zoomToSpan();
mTransitRouteLine = result.getRouteLines().get(0);
}
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(mContext, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
}
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
//起终点或途经点地址有岐义,通过以下接口获取建议查询信息
//result.getSuggestAddrInfo()
return;
}
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
mDrivingRouteOverlay = new DrivingRouteOverlay(mBaiduMap);
mBaiduMap.clear();
mBaiduMap.setOnMarkerClickListener(mDrivingRouteOverlay);
mDrivingRouteOverlay.setData(result.getRouteLines().get(0));
mDrivingRouteOverlay.addToMap();
mDrivingRouteOverlay.zoomToSpan();
mDrivingRouteLine=result.getRouteLines().get(0);
nodeIndex=-1;
}
}
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
}
};
public interface Navigation{
void navigation(HospitalInfo hospitalInfo);
}
public void setNavigation(Navigation navigation){
this.navigation=navigation;
}
}