1. 程式人生 > >百度地圖根據座標自定義覆蓋物,並實現其點選事件

百度地圖根據座標自定義覆蓋物,並實現其點選事件

在很多APP中都會用到百度地圖,以實現各種功能,本片文章介紹在百度地圖中新增自定義覆蓋物並新增其點選事件

public class NearByFragment extends Fragment {
    private MapView mapView;
    private BaiduMap Mmap;
    private boolean isFirstLoc = true;
    private HttpUtils utils;
    private BitmapDisplayConfig displayConfig;
    private BitmapUtils bmputils;
    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // 初始化控制元件,並開啟定位功能 View view = initView(inflater, container); //新增Marker覆蓋物的點選事件 Mmap.setOnMarkerClickListener(new OnMarkerClickListener(){ @Override public
boolean onMarkerClick(Marker marker) { Bundle extraInfo = marker.getExtraInfo(); final Grogshop shop = (Grogshop) extraInfo.getSerializable("Grogshop"); View v2 = LayoutInflater.from(getActivity()).inflate(R.layout.window2, null); TextView shop_name = (TextView) v2.findViewById(R.id.shop_name); TextView tv_distance = (TextView) v2.findViewById(R.id.distance); TextView price_range = (TextView) v2.findViewById(R.id.price_range); TextView worktime_window = (TextView) v2.findViewById(R.id.worktime_window); ImageView view_img = (ImageView) v2.findViewById(R.id.view_img); shop_name.setText(shop.getShopName()); bmputils.display(view_img, shop.getShopPhoto(), displayConfig); final
float distance_float = FunctionUtils.getDistance(shop, getActivity()); tv_distance.setText(distance_float + "km"); price_range.setText(shop.getPriceRange()); worktime_window.setText(shop.getBusinessTime()); v2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 載入新資料讓他跳轉到一個新的介面,展示資訊 Intent intent = new Intent(getActivity(), NearByDetail.class); intent.putExtra("Grogshop", shop); intent.putExtra("distance_int", distance_float); startActivity(intent); getActivity().overridePendingTransition(R.anim.next_in_anim, R.anim.next_out_anim); Mmap.hideInfoWindow(); } }); FunctionUtils.PopView(v2); int infoWindowYOffset = -30; final InfoWindow info = new InfoWindow(v2, marker.getPosition(), infoWindowYOffset); Mmap.showInfoWindow(info); return true; } }); //新增地圖的點選事件 Mmap.setOnMapClickListener(new OnMapClickListener() { @Override public void onMapClick(LatLng arg0) { Mmap.hideInfoWindow(); } @Override public boolean onMapPoiClick(MapPoi arg0) { // TODO Auto-generated method stub return false; } }); return view; } // 為地圖新增marker覆蓋物 private void getoverlay() { //為地圖新增自定義的覆蓋物 final BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.icon_green); utils.send(HttpMethod.POST, String.format(Constants.StringJson_shop, SPUtils.getString(getActivity(), "city", "")), new RequestCallBack<String>() { // 當請求失敗呼叫此方法 @Override public void onFailure(HttpException exception, String msg) { exception.printStackTrace(); Log.i("NearByFragment", msg); } // 當請求介面成功呼叫此方法 @Override public void onSuccess(ResponseInfo<String> info) { if (info.statusCode == 200) { String json = info.result; Log.e("NearByFragment", json); // 以下解析json/封裝list集合,再遍歷list新增覆蓋物 try { JSONObject obj = new JSONObject(json); String code = obj.getString("code"); String Message = obj.getString("message"); if (code.equals("000000")) { List<Grogshop> list = JsonUtils.JSON1Str(json); if (adapter == null) { if (getActivity() == null || getActivity().isDestroyed()) { return; } else { adapter = new ShopAdapter(getActivity()); adapter.addData(list); shop_list.setAdapter(adapter); } } else { adapter.addData(list); shop_list.setAdapter(adapter); } for (int i = 0; i < list.size(); i++) { Grogshop shop = list.get(i); // 利用bundle為覆蓋物新增資訊 Bundle bundle = new Bundle(); // 是否加盟 // String isJoining = shop.getIsJoining(); // 經緯度 double longitude = shop.getLongitude(); double dimensions = shop.getDimensions(); // 利用bundle逐一put進marker覆蓋物 bundle.putSerializable("Grogshop", shop); LatLng location = new LatLng(dimensions, longitude); op = new MarkerOptions().extraInfo(bundle).position(location).icon(bitmap); MarkerOptions().extraInfo(bundle).position(location).icon(bitmap2); // 將覆蓋物新增到百度地圖上 Mmap.addOverlay(op).setZIndex(i); } } else if (code.equals("000011")) { if (getActivity() == null || getActivity().isDestroyed()) { return; } else { Toast.makeText(getActivity(), Message, 0).show(); } } } catch (JSONException e) { e.printStackTrace(); } } } }); } private View initView(LayoutInflater inflater, ViewGroup container) { View view = inflater.inflate(R.layout.fragment_nearby, container, false); utils = new HttpUtils(5000); bmputils = new BitmapUtils(getActivity(), Constants.cachepath); displayConfig = new BitmapDisplayConfig(); displayConfig.setBitmapConfig(Config.ARGB_8888); AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f); animation.setDuration(500); displayConfig.setAnimation(animation); displayConfig.setLoadingDrawable(getActivity().getResources().getDrawable(R.drawable.loading_not)); displayConfig.setLoadFailedDrawable(getActivity().getResources().getDrawable(R.drawable.loading_not)); // 獲取地圖控制元件引用 mapView = (MapView) view.findViewById(R.id.bmapView); // 隱藏百度地圖縮放圖示 mapView.showZoomControls(false); // 獲取百度地圖控制元件 Mmap = mapView.getMap(); // 設定百度地圖縮放層級 Mmap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder().zoom(15).build())); //新增一個標誌位避免頻繁向伺服器發請求 設定一個定時器避免請求資料為空 if (isFirstLoc) { Timer timer = new Timer(); timer.schedule(new TimerTask() { @Override public void run() { getoverlay(); isFirstLoc = false; } }, 2500); } return view; } @Override public void onResume() { super.onResume(); // 實現地圖生命週期管理 mapView.onResume(); } @Override public void onPause() { super.onPause(); mapView.onPause(); } @Override public void onDestroy() { super.onDestroy(); mapView.onDestroy(); } } //百度定位監聽 /** *@author xuenan *百度定位監聽實現 */ public class MyLocationLister implements BDLocationListener{ private Context context; private BaiduMap Mmap; private boolean isFirstLoc = true; public MyLocationLister(BaiduMap mapView,Context context){ this.context = context; this.Mmap = mapView; } @Override public void onReceiveLocation(BDLocation location) { final MyLocationData data = new MyLocationData.Builder() .direction(location.getDirection()) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .accuracy(location.getRadius()) .build(); if(data!=null){ // 設定地圖的定位資料 try { // 設定地圖的定位資料 Mmap.setMyLocationData(data); } catch (NullPointerException e) { e.printStackTrace(); } } //Mmap.setMyLocationEnabled(true); //城市 String city = location.getCity(); //將經緯度和城市,街道資訊,存入SharedPreference中 SPUtils.putString(context,"latitude", String.valueOf(location.getLatitude())); SPUtils.putString(context,"longitude", String.valueOf(location.getLongitude())); SPUtils.putString(context,"city", String.valueOf(city)); SPUtils.putString(context, "AddrStr",location.getAddrStr()); // 更新地圖狀態 if (isFirstLoc) { isFirstLoc = false; LatLng pt = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(pt); Mmap.animateMapStatus(update); } } }