关于Android集成高德地图的那些事儿...地图标注Marker 、Poi 搜索_高德地图bitmapdescriptorfactory.fromview-程序员宅基地

技术标签: 地图  定位  Marker  

    上一篇博客介绍了地图的显示,不要太简单了, 我们要实现的效果当然不仅仅是显示地图这么废了, 现在地图是有了 , 怎么在地图上添加一个标注呢? 添加一个圆形?又或者是我想在地图上添加个自定义的View, 会不会很6? 呵呵 

    答案全在AMap aMap=mapView.getMap(); 我们添加的这些东西都是要在aMap上操作的 , 之前有说道定位回调的方法里面显示了系统的定位小蓝点 ,  那我不想要那个效果咋整 ? 当然了SDK中的api可以修改定位点的样式 , 这个就不说了 , 对于强迫症的我来说 , 不如自己搞一个来的痛快, 系统定位蓝点不过就是个Marker而已

    先来创建一个Marker ,  aMap.addMarker( ) 方法返回值就是Marker对象 , 方法参数接收的是 MarkerOptions  ; MarkerOptions 可以设置资源图片 , 设置锚点 ,标题等等 ;重点说下这个图片MarkerOptions有个icon()方法 可以加载资源图片 , 也可以加载自定义View ; 有趣的是 ,api现在已经支持给Marker添加动画了  , 那就在添加Marker的同时 加入缩放和透明度渐变的动画

// BitmapDescriptorFactory.fromView(View view);
// BitmapDescriptorFactory.fromBitmap(Bitmap bitmap)
// BitmapDescriptorFactory.fromAsset(String s);
// BitmapDescriptorFactory.fromResource(int res);
// BitmapDescriptorFactory.fromFile(String path);

//加载一个layout
View view = View.inflate(this, R.layout.item_marker, null);
((TextView) view.findViewById(R.id.num)).setText("layout");
Marker addMarker = aMap.addMarker(new MarkerOptions()
                       .icon(BitmapDescriptorFactory.fromView(view))
                       .position(new LatLng(locationInfo.getLatitude(), locationInfo.getLongitude())));

//Marker 开启动画
private void startAnimation(Marker marker) {
        AnimationSet animationSet = new AnimationSet(false);
        Animation scale = new ScaleAnimation(0, 1, 0, 1);
        Animation alpha = new AlphaAnimation(0, 1);
        animationSet.addAnimation(alpha);
        animationSet.addAnimation(scale);
        animationSet.setDuration(300);
        animationSet.setInterpolator(new LinearInterpolator());
        marker.setAnimation(animationSet);
        marker.startAnimation();
    }


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/ic_location_blue_small" />

    <TextView
        android:id="@+id/num"
        style="@style/text_10_color_1_style"
        android:layout_gravity="center"
        android:layout_marginBottom="6dp"
        android:text="5"
        android:textStyle="bold" />

</FrameLayout>

     这样就可以在地图上面显示我们自定义的Marker了 , 那现在问题来了 , 如果我点击Marker会有什么效果呢 ? Marker点击事件需要设置监听才能响应 , 那就实现点击Marker放大的效果吧

     非常坑爹的一个就是 , 并没有找到直接在原有Marker的基础上修改样式的方法 , 目前我是在点击Marker时 将之前的Marker移除掉 , 然后重新添加一个新的Marker , 以此来产生点击效果 , 

aMap.setOnMarkerClickListener(this);

 @Override
    public boolean onMarkerClick(Marker marker) {
        //step 1 , reset  将上次的marker恢复到初始状态
        if (markerPre != null && !marker.equals(markerPre) && markerIndexPre != -1) {
            markerPre.remove();
            View view = View.inflate(this, R.layout.item_marker, null);
            ((TextView) view.findViewById(R.id.num)).setText(String.valueOf(markerIndexPre));
            ((ImageView) view.findViewById(R.id.img)).setImageResource(R.drawable.ic_location_blue_small);

            markers.put(markerIndexPre, aMap.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory.fromView(view))
                    .position(markerPre.getPosition())));
        }
        if (!marker.equals(markerPre)) {
            int clickIndex = -1;
            for (Map.Entry<Integer, Marker> entry : markers.entrySet()) {
                if (marker.equals(entry.getValue())) {
                    clickIndex = entry.getKey();
                    break;
                }
            }
            if (clickIndex != -1) {
                marker.remove();
                View view = View.inflate(this, R.layout.item_marker, null);
                ((TextView) view.findViewById(R.id.num)).setText(String.valueOf(clickIndex));
                ((ImageView) view.findViewById(R.id.img)).setImageResource(R.drawable.ic_location_blue_big);
                Marker addMarker = aMap.addMarker(new MarkerOptions()
                        .icon(BitmapDescriptorFactory.fromView(view))
                        .position(marker.getPosition()));
                markers.put(clickIndex, addMarker);
                startAnimation(addMarker);
                recyclerView.smoothScrollToPosition(clickIndex - 1);
                locationAdapter.refreshIndex(clickIndex - 1);
                markerPre = addMarker;
                markerIndexPre = clickIndex;
            }
        }       
        return false;
    }
Caption

   

那地图上能不能绘制 圈儿圈儿 涅? 嘻嘻...api已经提供了方法 顾名思义 , 看到方法名就知道啥意思了

Circle circleOut = aMap.addCircle(new CircleOptions().center(latLng)//圈儿圈儿的坐标
                .radius(maxRadius)//圈儿圈儿的半径R
                .strokeColor(ContextCompat.getColor(this, android.R.color.transparent))
                .fillColor(ContextCompat.getColor(this, R.color.blue_sharder))
                .strokeWidth(0));

   就画个圈儿圈儿会不会太单调了呀 , 要不干脆把系统的小蓝点干掉吧 , 自己写个圈儿圈儿补上 , 思路:外圈儿 套 内圈儿 , 内圈儿套 中心点 , 外圈儿半径R和中心点固定 , 内圈做动画通过不断的改变半径来实现雷达扫描的效果

private void refreshMyLocation(LatLng latLng) {
        aMap.clear();
        clearCircle();
        // 绘制不同半径的圆,添加到地图上
        final Circle circleOut = aMap.addCircle(new CircleOptions().center(latLng)
                .radius(maxRadius)
                .strokeColor(ContextCompat.getColor(this, android.R.color.transparent))
                .fillColor(ContextCompat.getColor(this, R.color.blue_sharder))
                .strokeWidth(0));

        final Circle circleIn = aMap.addCircle(new CircleOptions().center(latLng)
                .radius(10)
                .strokeColor(ContextCompat.getColor(this, android.R.color.transparent))
                .fillColor(ContextCompat.getColor(this, R.color.green_sharder))
                .strokeWidth(0));

        aMap.addMarker(new MarkerOptions()
                .anchor(0.5f, 0.5f)
                .setFlat(true)//平贴地面
                .icon(BitmapDescriptorFactory.fromView(View.inflate(this, R.layout.item_location_point, null)))
                .position(latLng));

        listCircle.add(circleOut);
        listCircle.add(circleIn);
        startAnimator(circleIn);
    }

    private void clearCircle() {
        if (valueAnimator != null && valueAnimator.isRunning()) {
            valueAnimator.cancel();
        }
        for (int i = 0; i < listCircle.size(); i++) {
            listCircle.get(i).remove();
            listCircle.remove(i);
        }
    }

    private void startAnimator(final Circle circle) {
        valueAnimator = ValueAnimator.ofFloat(10, maxRadius).setDuration(3000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mAnimatorValue = (float) animation.getAnimatedValue();
                circle.setRadius(mAnimatorValue);//在这里改变半径 不断绘制
            }
        });
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);
        valueAnimator.setRepeatMode(ValueAnimator.REVERSE);//反向重复执行,可以避免抖动
        valueAnimator.start();
    }

  哈哈 , 还不错!  博客图片大小限制,压缩的有点模糊,凑活看吧

Caption

    接下就是Poi 搜索了 , 话说Poi是个啥? Point of Interest兴趣点 , 就是个关键词搜索 , 这个是sdk自带的接口 , 返回与我们的关键词相关的一些数据  

//查询条件
PoiSearch.Query(String keyWord,String p2,String cityCode) ; 

keyWord表示搜索字符串

第二个参数表示POI搜索类型,二者选填其一,选用POI搜索类型时建议填写类型代码,码表可以参考下方(而非文字)

cityCode表示POI搜索区域,可以是城市编码也可以是城市名称,也可以传空字符串,空字符串代表全国在全国范围内进行搜索

//创建查询条件
PoiSearch.Query query = new PoiSearch.Query(keyWord, "", "");
if (poiSearch == null) {
                        poiSearch = new PoiSearch(MapActivity.this, query);
                        poiSearch.setOnPoiSearchListener(new                 PoiSearch.OnPoiSearchListener() {
                            @Override
                            public void onPoiSearched(PoiResult poiResult, int i) {
                                //搜索结果在这里解析
                                PoiItem poiItem = poiResult.getPois().get(j);
                                LatLonPoint latLonPoint = poiItem.getLatLonPoint();
                                latLng = new LatLng(latLonPoint.getLatitude(),             
                                latLonPoint.getLongitude());
                                locationInfo = new LocationInfo();
                                locationInfo.setLatitude(latLonPoint.getLatitude());
                                locationInfo.setLongitude(latLonPoint.getLongitude());
                                locationInfo.setAddressName(poiItem.getTitle());
                                locationInfo.setAddress(poiItem.getAdName());
                            }

                            @Override
                            public void onPoiItemSearched(PoiItem poiItem, int i) {

                            }
                        });
                    } else {
                        poiSearch.setQuery(query);
                    }
//开始异步搜索
 poiSearch.searchPOIAsyn();

  可以从PoiItem  中获取坐标信息,  poiItem.getTitle() 是Poi的名称 ,关于PoiItem的详细api可以参开开放平台的api的文档http://a.amap.com/lbs/static/unzip/Android_Map_Doc/index.html

  来展示下最终的效果

 

Caption

  GiHub地址:https://github.com/good-good-study/StudyApplication

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/sxt_zls/article/details/84330975

智能推荐

python输出1到100整数_python第一个代码程序打印1到100整数-程序员宅基地

文章浏览阅读9.8k次。原博文2019-05-30 07:36 −def main(): #打印1到100的整数 i=1 while i_输出一到一百所有的数python

LoRa---SX1278_sx1278 fsk配置-程序员宅基地

文章浏览阅读2.4k次。LoRa—SX1278LoRa介绍sx1278支持FSK GFSK MSK GMSK LoRa OOK调制,分为Lora调制解调器和FSK/OOK调制解调器,根据选定的模式可以选择传统的FSK/OOK调制技术或Lora扩频调制技术,这里对主要对Lora模式做一些介绍。LoRa是一个基于线性调制的无线网络标准。线性调制:线性调制代表“压缩高强度雷达脉冲”,这是一个频率随时间增加或者减少的..._sx1278 fsk配置

mtk6592处理器怎么样,mtk6592参考设计原理图下载-程序员宅基地

文章浏览阅读2.9k次。首先说一下mtk6592,联发科MT6592是联发科股份科技有限公司基于2013年7月份发布的的全球首款商用量产同步八核智能机系统单芯片。MT6592由8颗Cortex-A7核心构成,采用台积电28nm工艺,最高频率可达2GHz。MT6592的GPU已确认为mail 450 mp4,该GPU同频率同核心性能约为mail 400 mp4的两倍,GPU频率性能ARM Mali450-MP4图像内核,7..._mtk6592

大一计算机基础实用教程答案第二章,《计算机基础实用教程》答案-程序员宅基地

文章浏览阅读103次。第1章1.4.习题1. 电子计算机的发展阶段通常以构成计算机的电子器件来划分,至今已经历了四代,目前正在向第五代过渡。1.第一代——电子管计算机(1946-1957年)第一代计算机采用的主要原件是电子管,称为电子管计算机。2.第二代——晶体管计算机(1958-1964年)晶体管的发明给计算机技术的发展带来了革命性的变化。第二代计算机采用的主要元件是晶体管,称为晶体管计算机。3.第三代——集成电路计..._计算机实用技术大一

PyCharm中代码块的展开和折叠快捷键_pycharm展开所有代码-程序员宅基地

文章浏览阅读944次。PyCharm中代码块的展开和折叠快捷键_pycharm展开所有代码

获取当前路由地址_能获取前端路由的地址吗-程序员宅基地

文章浏览阅读103次。【代码】获取当前路由地址。_能获取前端路由的地址吗

随便推点

关于三角形外心性质的探究_三角形外心的性质-程序员宅基地

文章浏览阅读3k次。标题 三角形外心的性质 当还在高中的你遇到三角形外心性质的问题,是不是很揪心呢 那今天就交给大家一个绝招,hhhhhhh16340129 数据科学与计算机学院 一、三角形外心问题? 1.这个不多说啦,有图有问题,而且这类题一般都和向量有关 在这里涉及在外心O,即三角形三边的垂直平分线的交点,下面我..._三角形外心的性质

forEach循环报java.lang.NullPointerException空指针异常_集合不为空,foreach报空指针异常-程序员宅基地

文章浏览阅读1.1k次。一、测试1、最近对接SDK 发现项目中List<Object>.forEach老报空指针异常这个原因是因为返回的时候返回为null导致遍历出错。 public static void main(String[] args) { List<String> strings = null; strings.forEach(System.out::println); }..._集合不为空,foreach报空指针异常

handle句柄 matlab_谁能系统且简要地介绍一下matlab中的function handle吗?-程序员宅基地

文章浏览阅读327次。如果你学过其他的编程语言的话,类比可能会让你更好地理解什么是function handle(“函数句柄”,这个翻译太难听了)。Matlab里面function handle类似于python里面的函数对象、C++语言里面的函数指针、Perl里面的函数引用。function handle可以将function包装操作成(handle)一个变量。一个函数变成了一个变量之后,则我们可以在另外一个函数的参..._matlab中fun4_1是什么

【Redis】GEO(地理坐标)数据结构_geo坐标-程序员宅基地

文章浏览阅读2.1k次,点赞2次,收藏2次。GEO就是Geolocation的简写形式,代表地理坐标。Redis在3.2版本中加入了对GEO的支持,允许存储地理坐标信息,帮助我们根据经纬度来检索数据。一个点评平台,有多个频道,每个频道都有很多个店铺。我们按照频道类型做分组,相同频道的店铺作为同一组,以频道id为key存入同一个GEO集合中即可。其中,Value值存储店铺的id。_geo坐标

Hexo博客中使用标签云hexo-tag-cloud_hexo插入标签云-程序员宅基地

文章浏览阅读9.6k次。Hexo博客中使用标签云hexo-tag-cloud在github上标签云使用教程中,按照步骤添加配置后不能添加标签云,后考虑解决方案如下: 1. 添加依赖问题 - go into your hexo system folder, and add depandence “hexo-tag-cloud”: “2.0.*” to package.json在根目录文件package.json文件_hexo插入标签云

Spring Boot 注入外部配置到应用内部的静态变量_springboot环境变量怎么动态注入到静态变量中-程序员宅基地

文章浏览阅读454次。Spring Boot允许你外部化你的配置,这样你就可以在不同的环境中使用相同的应用程序代码,你可以使用properties文件、YAML文件、环境变量和命令行参数来外部化配置,属性值可以通过使用@Value注解直接注入到你的bean中,通过Spring的Environment抽象访问,或者通过@ConfigurationProperties绑定到结构化对象。那么如何进行Spring Boot ..._springboot环境变量怎么动态注入到静态变量中