android 百度地图定位与覆盖物的添加,以及他们的点击事件

Android 百度地图定位与覆盖物的添加,以及他们的点击事件。

android 百度地图定位与覆盖物的添加,以及他们的点击事件_第1张图片

那个小红点就是覆盖物,小蓝点是定位图标。

(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;
        }
    });

你可能感兴趣的:(Android,studio,Android地图)