在开发的过程中发现一个问题,当你完成一个新的功能时,过一段时间就会淡忘,所以想通过写博客的方式来总结记录下,以后有类似的功能还能参考下,简单明了。
前一阵因为写公司一个项目,有一个功能是在范围内进行标注,简单来说就是在当前位置附近XX米范围之内点击地图进行标注,超出范围则不能进行标注,现在把它抽取出来写成个demo。
1.效果图
1.范围内添加标注:
2.超出范围添加标注:
2.实现思路
首先要获取当前位置的坐标:获取当前位置使用定位即可;
其次要判断当前位置是否在范围内,在范围内添加标注,超出范围提示用户,在百度地图的计算工具里面有一个判断空间关系的方法:
//判断点pt是否在,以pCenter为中心点,radius为半径的圆内。
SpatialRelationUtil.isCircleContainsPoint(pCenter, radius, pt);
最后实现点击地图添加标注:在地图中添加单击监听事件OnMapClickListener,使用百度地图sdk中的标注覆盖物即可实现;
3.代码
废话不多说,直接上代码:
<1>布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xzx.baidumapdemo.MainActivity" >
<com.baidu.mapapi.map.MapView
android:id="@+id/bdMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" />
<LinearLayout
android:id="@+id/ll_bigorsmall"
android:layout_width="44dip"
android:layout_height="88dip"
android:orientation="vertical"
android:layout_marginRight="15dip"
android:layout_marginBottom="25dip"
android:layout_alignParentRight=

本文介绍了如何在Android应用中实现在百度地图上根据范围进行标注。首先展示功能的效果图,接着详细解释实现思路,包括获取当前位置坐标、判断是否在范围内以及添加标注的逻辑。最后,提供了核心代码片段,说明使用定位精度来表示标注范围,并欢迎大家交流优化方案。
7318

被折叠的 条评论
为什么被折叠?



