android 百度地圖定位與覆蓋物的添加,以及他們的點擊事件

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/huangf321123/article/details/50317963

Android 百度地圖定位與覆蓋物的添加,以及他們的點擊事件。

這裏寫圖片描述

那個小紅點就是覆蓋物,小藍點是定位圖標。

(1)百度地圖定位的核心代碼:
//定位初始化

public void initLocationClient() {
    LocationClientOption option = new LocationClientOption();
    option.setCoorType(bd09ll);// 返回的定位結果是百度經緯度,默認值gcj02
    option.setLocationMode(LocationMode.Hight_Accuracy);// 設置定位模式
    // 設置發起定位請求的間隔時間
    option.setScanSpan(Iconstants.scanSpan);
    option.setOpenGps(true);
    option.setIsNeedAddress(true);
    option.setAddrType("all");//返回的定位結果包含地址信息  
    mLocationClient.setLocOption(option);
}

//把定位圖標展示在地圖上
public void show(){

bMapView = (MapView)layout.findViewById(R.id.bmapView);
    mBaiduMap = bMapView.getMap();
mBaiduMap.setMyLocationEnabled(true);// 開啓定位圖層
    mBuilder = new MyLocationData.Builder();
    MyLocationData locData = mBuilder
            .accuracy(mLocation.getRadius())
            // 此處設置開發者獲取到的方向信息,順時針0-360
            .direction(mLocation.getDirection())
            .latitude(mLocation.getLatitude())
            .longitude(mLocation.getLongitude()).build();
    mBaiduMap.setMyLocationData(locData);

}
(2)百度地圖覆蓋物的添加
//通過調用這個方法就能添加覆蓋物

public void getMaker(final Bundle bundle){
    mSearch.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {

        @Override
        public void onGetReverseGeoCodeResult(ReverseGeoCodeResult arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onGetGeoCodeResult(GeoCodeResult result) {
            // TODO Auto-generated method stub
            if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
                Log.e(tag, "onGetGeoCodeResult");
                Toast.makeText(getActivity(), "抱歉", Toast.LENGTH_LONG).show();
                return;
            }
            addOverlay(bundle);
        }
    });
}

public void addOverlay(final Bundle bundle){
//先獲得你要在地圖上添加覆蓋物的經緯度

LatLng ll = new LatLng(bundle.getDouble("lat"),bundle.getDouble("lng"));

//在經緯度爲ll的地圖上添加圖標bdA,並設置爲不可拖動,還可以攜帶數據bundle,這個bundle存放有我從json中獲取的數據

            OverlayOptions ooA = new MarkerOptions().position(ll).icon(bdA)
                    .draggable(false).extraInfo(bundle);

//覆蓋物添加到了地圖上

    mMarker = (Marker) (mBaiduMap.addOverlay(ooA));

}
(3)定位小圖標,與覆蓋物的點擊事件
//覆蓋物的點擊

mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(final Marker maker) {
            // TODO Auto-generated method stub
            if (routeOverlay != null ) {
                routeOverlay.removeFromMap();
            }
            Log.e(tag, ">>>>"+maker+"LLLLLLLL"+maker.getExtraInfo());
            if(maker.getExtraInfo() == null || maker.getExtraInfo().getString("name") == null){
                Log.e(tag, ">>>>"+maker);

            }else{
                mBaiduMap.hideInfoWindow();
                PlanNode stNode = PlanNode.withLocation(new LatLng(AppContext.getInstance().getLatitude(), AppContext.getInstance().getLongitude()));
                PlanNode enNode = PlanNode.withLocation(new LatLng(maker.getExtraInfo().getDouble("lat"),maker.getExtraInfo().getDouble("lng")));
                mRouteSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode).to(enNode));
                //點擊maker彈出的詳情框
                View view  = LayoutInflater.from(getActivity()).inflate(R.layout.show_overlay_info, null);
                TextView startTime = (TextView) view.findViewById(R.id.overlay_time_start);
                TextView weight = (TextView) view.findViewById(R.id.overlay_weighgt);
                TextView volume = (TextView) view.findViewById(R.id.overlay_volume);
                TextView type = (TextView) view.findViewById(R.id.overlay_car_type);
                TextView count = (TextView) view.findViewById(R.id.tv_count);
                int num = 0;
                for(String s:listCity){
                    if(s.equals(maker.getExtraInfo().getString("name"))){
                        num ++;
                    }
                }
                count.setVisibility(View.VISIBLE);
                if(num == 1){
                    count.setVisibility(View.GONE);
                }else{
                    count.setVisibility(View.VISIBLE);
                    count.setText("("+num+")");
                }
                startTime.setText(maker.getExtraInfo().getString("requiredSendDt"));
                weight.setText(maker.getExtraInfo().getString("weight")+"噸");
                volume.setText(maker.getExtraInfo().getString("volume")+"立方");
                type.setText(maker.getExtraInfo().getString("typeName"));
                LinearLayout intoDetail = (LinearLayout) view.findViewById(R.id.overlay_detail);
                mGoodsId = maker.getExtraInfo().getString("id");
                LatLng ll = maker.getPosition();
                InfoWindow infoWin = new InfoWindow(view, ll, -30 );
                mBaiduMap.showInfoWindow(infoWin);
                //點擊查看當前貨源詳情
                intoDetail.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub

                        Intent i = new Intent(getActivity(),GoodsActivity.class);
                           i.putExtra("city", maker.getExtraInfo().getString("startArea"));
                           getActivity().startActivity(i);
                           getActivity().overridePendingTransition(R.anim.slide_in_left,R.anim.fade_in_exit);

                        mBaiduMap.hideInfoWindow();
                        if (routeOverlay != null ) {
                            routeOverlay.removeFromMap();
                        }
                    }
                });
            }
            return true;
        }
    });

//定位小圖標的點擊事件

mBaiduMap.setOnMyLocationClickListener(new OnMyLocationClickListener() {

        @Override
        public boolean onMyLocationClick() {
            // TODO Auto-generated method stub
            UIHelper.showToastsShort("您當前的位置:"+AppContext.getInstance().getAddress());
            return false;
        }
    });
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章