android 百度地图API 使用Marker和InfoWindow

前言:在android开发过程中,百度地图的使用是比较普遍的,但是如何使用,使用什么版本的百度API还是需要一些讲究。

在项目过程中,需要用到百度地图的marker和InfoWindow的功能。

标注覆盖物(百度地图官方图)

basicmap1.png

布局文件很简单,主要就是mapview,如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/overall_bg">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/gps_button_reset"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/gps_reset_button"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="16dp"
            />

        <Button
            android:id="@+id/gps_button_history"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/gps_history_button"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="16dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="16dp"
            />
    </LinearLayout>

    <include layout="@layout/separate_line" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/gps_text"

        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:textSize="15sp"
        android:layout_gravity="center"
        android:gravity="center"
        />

    <include layout="@layout/separate_line" />

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

</LinearLayout>


标注覆盖物实现代码如下:
    /**
     * 根据手表的经纬度在地图上设置位置,更新顶部的位置文字描述
     */
    private void updateLocation(ArrayList<GPSBean> list) {
        /**
         * 1. 新用户默认显示地址
         */
        double lg = 104.06; // 成都市的经纬度
        double lt = 30.67;
        String location = getResources().getString(R.string.fake_position);

        baiduMap.clear();

        List<LatLng> potions = new ArrayList<>();

        for(int i = list.size() -1; i >=0 ; i--){
            // gps 非空,则设置经纬度、位置的文字描述
            lg = Double.parseDouble(list.get(i).getLongitude());
            lt = Double.parseDouble(list.get(i).getLatitude());
            location = list.get(i).getAddress();
            //地址太长,处理下
            location = location.replace("四川省","").replace("成都市","").replace(".","");

            final LatLng ll = new LatLng(lt, lg); // 注意经纬度顺序

            /**
             * 为每个足迹依据先后顺序编号
             */
            int image_id = 0;
            switch (i){
                case 9: image_id = R.mipmap.icon_mark1;break;
                case 8: image_id = R.mipmap.icon_mark2;break;
                case 7: image_id = R.mipmap.icon_mark3;break;
                case 6: image_id = R.mipmap.icon_mark4;break;
                case 5: image_id = R.mipmap.icon_mark5;break;
                case 4: image_id = R.mipmap.icon_mark6;break;
                case 3: image_id = R.mipmap.icon_mark7;break;
                case 2: image_id = R.mipmap.icon_mark8;break;
                case 1: image_id = R.mipmap.icon_mark9;break;
                case 0: image_id = R.mipmap.icon_mark10;break;
            }
            BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(image_id);
            OverlayOptions options = new MarkerOptions().position(ll).icon(descriptor).zIndex(i);
            Marker marker = (Marker) baiduMap.addOverlay(options);
            //为marker添加识别标记信息
            Bundle bundle = new Bundle();
            bundle.putSerializable("info", list.get(i));
            marker.setExtraInfo(bundle);

            MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll,17);
            baiduMap.setMapStatus(update);
        }

为标记的marker添加监听,点击时实现弹出infowindow。(infowindow每次最多显示一条信息)


        /**
         * 为标记添加监听
         * 点击弹出地理位置信息
         */
        baiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                // 获得marker中的数据
                GPSBean info = (GPSBean) marker.getExtraInfo().get("info");
                // 生成一个TextView用户在地图中显示InfoWindow
                TextView location = new TextView(getApplicationContext());
                location.setBackgroundResource(R.mipmap.gps_button);
                location.setPadding(15, 15, 8, 35);
                location.setTextColor(Color.DKGRAY);
                location.setText("定位时间:" + info.getUploadTime() + "\n" + info.getAddress());
                location.setTextSize(12);
                // 将marker所在的经纬度的信息转化成屏幕上的坐标
                final LatLng ll = marker.getPosition();
                Point p = baiduMap.getProjection().toScreenLocation(ll);
                p.y -= 47;
                LatLng llInfo = baiduMap.getProjection().fromScreenLocation(p);
                // 为弹出的InfoWindow添加点击事件
                mInfoWindow = new InfoWindow(location,llInfo, new InfoWindow.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick() {
                        baiduMap.hideInfoWindow();
                    }
                });
                // 显示最后一条的InfoWindow
                baiduMap.showInfoWindow(mInfoWindow);
                return true;
            }
        });

最后将地图显示到最新的数据,并且显示Infowindow。
    /**
     * 显示最后一个地理位置信息
     * 创建InfoWindow展示的view
     */
    private void updateBaidumapInfowindown(String location,double lt, double lg){
        TextView textView = new TextView(getApplicationContext());
        textView.setBackgroundResource(R.mipmap.gps_button);
        textView.setPadding(15, 15, 8, 35);
        textView.setTextColor(Color.DKGRAY);
        textView.setText(location);
        textView.setTextSize(12);
        final LatLng ll = new LatLng(lt,lg);
        mInfoWindow = new InfoWindow(textView, ll, new InfoWindow.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick() {
                baiduMap.hideInfoWindow();
            }
        });
        //显示InfoWindow
        baiduMap.showInfoWindow(mInfoWindow);
        /**
         * 将地图视野移动最新的位置
         */
        MapStatusUpdate update = MapStatusUpdateFactory.newLatLngZoom(ll,17);
        baiduMap.setMapStatus(update);
    }

至此可以完成。但是还有最重要的一点,则是百度Api版本的问题,本方法在新版本 3.4.1 似乎就不能用这种方法实现,只能使用3.0.0版本。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值