【Android】3.9 覆盖物功能

分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04

一、简介

百度地图SDK所提供的地图等级为3-19级(3.7.1版本中有些部分已经提供到了21级),所包含的信息有建筑物、道路、河流、学校、公园等内容。百度将所有叠加或覆盖到地图的内容统称为地图覆盖物。如标注、矢量图形元素(包括:折线、多边形和圆等)、定位图标等。覆盖物拥有自己的地理坐标,当拖动或缩放地图时,它们会相应的移动。

百度地图SDK 3.7.1支持多种地图覆盖物。该版本所支持的地图覆盖物有:定位图层、地图标注(Marker)、几何图形(点、折线、弧线、多边形等)、地形图图层、POI检索结果覆盖物、线路规划结果覆盖物、热力图图层、瓦片图层以及自定义标注等。

1、标注覆盖物

(1)标注

开发者可根据自己实际的业务需求,利用标注覆盖物,在地图指定的位置上添加标注信息。

(2)底图标注

自v3.6.0版本起,SDK在BaiduMap提供了控制底图标注的ShowMapPoi方法,默认显示底图标注。利用此方法可得到仅显示道路信息的地图。

2、几何图形覆盖物

地图SDK提供多种结合图形覆盖物,利用这些图形,可帮助您构建更加丰富多彩的地图应用。目前提供的几何图形有:点(Dot)、折线(Polyline)、弧线(Arc)、圆(Circle)、多边形(Polygon)。

3、文字覆盖物

文字,在地图中也是一种覆盖物,开发者可利用相关的接口,快速实现在地图上书写文字的需求。

4、弹出窗覆盖物

利用弹出窗覆盖物可构建具有更强交互性的地图页面。

5、地形图图层

地形图图层(GroundOverlay),又可叫做图片图层,即开发者可在地图的指定位置上添加图片。该图片可随地图的平移、缩放、旋转等操作做相应的变换。该图层是一种特殊的Overlay, 它位于底图和底图标注层之间(即该图层不会遮挡地图标注信息)。

6、检索结果覆盖物

针对检索功能模块(POI检索、线路规划等),地图SDK还对外提供相应的覆盖物来快速展示结果信息。这些方法都是开源的,开发者可根据自己的实际去求来做个性化的定制。

二、运行截图

简介:介绍添加覆盖物并响应点击功能和弹出pop功能

详述:

(1)在popup中添加弹出自定义View的示例;

(2)点击周围4个Marker,弹出popup,可更新marker位置和更新marker图标;

(3)中间显示的为图片图层,该图层可随地图的移动、缩放等做出相应的操作;

(4)支持Maker设置透明度的方法,利用滑动条可以展示maker透明度变化;

(5)支持Marker设置动画的能力,包含两种方式:从地上生长和从天上落下。

本示例运行截图如下:

image

三、设计步骤

1、添加demo09_overlay.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/clear"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:padding="10dip"
            android:text="清除(clear)" />

        <Button
            android:id="@+id/resert"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:text="重置(reset)" />
    </LinearLayout>
     <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

         <SeekBar
             android:id="@+id/alphaBar"
             android:layout_marginBottom="2dip"
             android:layout_marginLeft="2dip"
             android:layout_marginRight="2dip"
             android:layout_marginTop="2dip"
             android:layout_weight="1"
             android:layout_width="155dp"
             android:max="10"
             android:progress="10"
             android:layout_height="wrap_content" />

        <CheckBox
            android:id="@+id/animation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:checked="true"
            android:text="Marker添加动画" />
    </LinearLayout>

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

</LinearLayout>

2、添加Demo09Overlay.cs文件

在SrcSdkDemos文件夹下添加该文件,然后将其内容改为下面的代码:

using Android.App;
using Android.Content.PM;
using Android.Graphics;
using Android.OS;
using Android.Widget;
using Com.Baidu.Mapapi.Map;
using Com.Baidu.Mapapi.Model;
using System.Collections.Generic;

namespace BdMapV371Demos.SrcSdkDemos
{
    /// <summary>
    /// 演示覆盖物的用法
    /// </summary>
    [Activity(Label = "@string/demo_name_overlay",
        ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
         ScreenOrientation = ScreenOrientation.Sensor)]
    public class Demo09Overlay : Activity
    {
        private TextureMapView mMapView;
        private BaiduMap mBaiduMap;
        private Marker mMarkerA;
        private Marker mMarkerB;
        private Marker mMarkerC;
        private Marker mMarkerD;
        private CheckBox animationBox = null;

        // 初始化全局 bitmap 信息,不用时及时 Recycle
        BitmapDescriptor bdA = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_marka);
        BitmapDescriptor bdB = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_markb);
        BitmapDescriptor bdC = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_markc);
        BitmapDescriptor bdD = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_markd);
        BitmapDescriptor bd = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_gcoding);
        BitmapDescriptor bdGround = BitmapDescriptorFactory.FromResource(Resource.Drawable.ground_overlay);

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.demo09_overlay);

            var clear = FindViewById<Button>(Resource.Id.clear);
            clear.Click += delegate
            {
                ClearOverlay();
            };

            var resert = FindViewById<Button>(Resource.Id.resert);
            resert.Click += delegate
            {
                ClearOverlay();
                InitOverlay();
            };

            var alphaSeekBar = FindViewById<SeekBar>(Resource.Id.alphaBar);
            alphaSeekBar.ProgressChanged += (s, e) =>
            {
                float alpha = e.Progress / 10.0f;
                if (mMarkerA != null) mMarkerA.Alpha = alpha;
                if (mMarkerB != null) mMarkerB.Alpha = alpha;
                if (mMarkerC != null) mMarkerC.Alpha = alpha;
                if (mMarkerD != null) mMarkerD.Alpha = alpha;
            };

            animationBox = FindViewById<CheckBox>(Resource.Id.animation);
            mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView);
            mBaiduMap = mMapView.Map;
            mBaiduMap.SetMapStatus(MapStatusUpdateFactory.ZoomTo(14.0f));
            InitOverlay();
            mBaiduMap.MarkerClick += (sender, args) =>
            {
                Marker marker = args.P0;
                Button button = new Button(ApplicationContext);
                button.SetTextColor(Color.Black);
                button.SetBackgroundResource(Resource.Drawable.popup);
                if (marker == mMarkerA || marker == mMarkerD)
                {
                    button.Text = "更改位置";
                    button.Click += (s, e) =>
                    {
                        LatLng latLng = args.P0.Position;
                        LatLng latLngNew = new LatLng(latLng.Latitude + 0.005,
                                latLng.Longitude + 0.005);
                        mBaiduMap.HideInfoWindow();
                    };
                    var mInfoWindow = new InfoWindow(button, marker.Position, -47);
                    mBaiduMap.ShowInfoWindow(mInfoWindow);
                }
                else if (marker == mMarkerB)
                {
                    button.Text = "更改图标";
                    button.Click += delegate
                    {
                        marker.Icon = bd;
                        mBaiduMap.HideInfoWindow();
                    };
                    var mInfoWindow = new InfoWindow(button, marker.Position, -47);
                    mBaiduMap.ShowInfoWindow(mInfoWindow);
                }
                else if (marker == mMarkerC)
                {
                    button.Text = "删除";
                    button.Click += delegate
                    {
                        marker.Remove();
                        mBaiduMap.HideInfoWindow();
                    };
                    var mInfoWindow = new InfoWindow(button, marker.Position, -47);
                    mBaiduMap.ShowInfoWindow(mInfoWindow);
                }
            };
        }

        public void InitOverlay()
        {
            // add marker overlay
            LatLng latLngA = new LatLng(39.963175, 116.400244);
            LatLng latLngB = new LatLng(39.942821, 116.369199);
            LatLng latLngC = new LatLng(39.939723, 116.425541);
            LatLng latLngD = new LatLng(39.906965, 116.401394);

            OverlayOptions ooA = new MarkerOptions()
                .InvokePosition(latLngA)
                .InvokeIcon(bdA)
                .InvokeZIndex(9)
                .Draggable(true);
            if (animationBox.Checked)
            {
                //掉下动画
                ((MarkerOptions)ooA).InvokeAnimateType(MarkerOptions.MarkerAnimateType.Drop);
            }
            mMarkerA = (Marker)(mBaiduMap.AddOverlay(ooA));

            OverlayOptions ooB = new MarkerOptions()
                .InvokePosition(latLngB)
                .InvokeIcon(bdB)
                .InvokeZIndex(5);
            if (animationBox.Checked)
            {
                //掉下动画
                ((MarkerOptions)ooB).InvokeAnimateType(MarkerOptions.MarkerAnimateType.Drop);
            }
            mMarkerB = (Marker)(mBaiduMap.AddOverlay(ooB));

            OverlayOptions ooC = new MarkerOptions()
                .InvokePosition(latLngC)
                .InvokeIcon(bdC)
                .Perspective(false)
                .Anchor(0.5f, 0.5f)
                .InvokeRotate(30)
                .InvokeZIndex(7);
            if (animationBox.Checked)
            {
                //生长动画
                ((MarkerOptions)ooC).InvokeAnimateType(MarkerOptions.MarkerAnimateType.Grow);
            }
            mMarkerC = (Marker)(mBaiduMap.AddOverlay(ooC));

            IList<BitmapDescriptor> giflist = new List<BitmapDescriptor>();
            giflist.Add(bdA);
            giflist.Add(bdB);
            giflist.Add(bdC);
            OverlayOptions ooD = new MarkerOptions()
                .InvokePosition(latLngD)
                .InvokeIcons(giflist)
                .InvokeZIndex(0)
                .InvokePeriod(10);
            if (animationBox.Checked)
            {
                //生长动画
                ((MarkerOptions)ooD).InvokeAnimateType(MarkerOptions.MarkerAnimateType.Grow);
            }
            mMarkerD = (Marker)(mBaiduMap.AddOverlay(ooD));

            // add ground overlay
            LatLng southwest = new LatLng(39.92235, 116.380338);
            LatLng northeast = new LatLng(39.947246, 116.414977);
            LatLngBounds bounds = new LatLngBounds.Builder()
                .Include(northeast)
                .Include(southwest).Build();
            OverlayOptions ooGround = new GroundOverlayOptions()
                    .PositionFromBounds(bounds)
                    .InvokeImage(bdGround)
                    .InvokeTransparency(0.8f);
            mBaiduMap.AddOverlay(ooGround);

            MapStatusUpdate u = MapStatusUpdateFactory.NewLatLng(bounds.Center);
            mBaiduMap.SetMapStatus(u);
            mBaiduMap.MarkerDrag += (s, e) => { };
            mBaiduMap.MarkerDragStart += (s, e) => { };
            mBaiduMap.MarkerDragEnd += (s, e) =>
            {
                Marker marker = e.P0;
                Toast.MakeText(this,
                        "拖拽结束,新位置:"
                        + marker.Position.Latitude + ", "
                        + marker.Position.Longitude,
                        ToastLength.Long).Show();
            };
        }

        /// <summary>
        /// 清除所有Overlay
        /// </summary>
        public void ClearOverlay()
        {
            mBaiduMap.Clear();
            mMarkerA = null;
            mMarkerB = null;
            mMarkerC = null;
            mMarkerD = null;
        }

        protected override void OnPause()
        {
            mMapView.OnPause();
            base.OnPause();
        }

        protected override void OnResume()
        {
            mMapView.OnResume();
            base.OnResume();
        }

        protected override void OnDestroy()
        {
            mMapView.OnDestroy();
            base.OnDestroy();
            // 回收 bitmap 资源
            bdA.Recycle();
            bdB.Recycle();
            bdC.Recycle();
            bdD.Recycle();
            bd.Recycle();
            bdGround.Recycle();
        }
    }
}

3、修改MainActivity.cs

在MainActivity.cs文件的demos字段定义中,去掉【示例9】下面的注释。

运行观察结果。

posted @ 2016-02-04 13:55  rainmj  阅读(1137)  评论(0编辑  收藏  举报