編輯:關於Android編程
``` /** * 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 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 arrayList = new ArrayList<>(); ArrayList 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; } }
先給大家展示下效果圖:package com.lixu.circlemenu; import android.app.Activity; import android.o
Android和java啟動的區別不同於使用 main() 方法啟動應用的其他編程范例,Android 系統會通過調用對應於其生命周期中特定階段的特定回調方法在 Acti
最近幫朋友做了一個動畫菜單,感覺有一定的實用價值,就在此給大家分享一下,先看看效果:源碼下載地址在末尾實現思路:從圖中可以看出,這三個(或更多,需要自己再實現)菜單是圍繞
本篇文章包括以下內容: JSON是什麼 JSONObject的解析和存儲 JSONObject的解析和存儲(抽象) JSONArray的解析和存儲 模擬周邊加油站JSO