这个百度地图 android SDK 关于基础地图覆盖物的例子php
//定义Maker坐标点 LatLng point = new LatLng(39.963175, 116.400244); //构建Marker图标 BitmapDescriptor bitmap = BitmapDescriptorFactory .fromResource(R.drawable.icon_marka); //构建MarkerOption,用于在地图上添加 Marker OverlayOptions option = new MarkerOptions() .position(point) .icon(bitmap); //在地图上添加Marker,并显示 mBaiduMap.addOverlay(option);
其中的Marker 也就是标记的意思,就是我们今天要说的覆盖物,其中的难点在于它只接收一个bitmap来显示,那假如当咱们有需求,须要加入一些复杂的View(好比Linearlayout嵌套一个TextView和ImageView)来代替这个bitmap怎么办?在我查了不少资料之后,终于找到了解决方案。web
其实解决方案很简单,就是将View或者Viewgroup转化为Bitmap就好了。由于地图的SDK是第三方提供的,咱们没有权利要求别人去改接口来适应咱们,因此咱们只好想办法去适应他们。ide
如今我自定义了一个布局,以下所示函数
point.xml布局
<?xml version="1.0" encoding="utf-8"?>测试
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="@drawable/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">动画<ImageView android:layout_width="35dp"
android:layout_height="35dp"
android:scaleType="centerCrop"
android:padding="5dip"
android:src="@drawable/head_1"/>ui<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android :color/black"
android:textSize="20sp"
android:text="测试"
/>spa</LinearLayout>
而后想让它转为Bitmap以后,装进Marker。下面是核心转换函数:
private Bitmap getViewBitmap(View addViewContent) {
addViewContent.setDrawingCacheEnabled(true);
addViewContent.measure(
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
addViewContent.layout(0, 0,
addViewContent.getMeasuredWidth(),
addViewContent.getMeasuredHeight());addViewContent.buildDrawingCache();
Bitmap cacheBitmap = addViewContent.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);return bitmap;
}
其中,最外层的布局我用Relativelayout尝试过,但没法转化获得bitmap,深层次的缘由有兴趣你们能够试试。
只要最外层是LinearLayout,里面的布局你能够随意写,无论再嵌套多少层其余的布局,好比Relativelayout,都能顺利的显示到地图上。
实现动画的前提是,你要拿到当前只知道经纬度的这一点在屏幕上的位置,拿到以后你就能够作一个假动画,把覆盖物放到目标位置以后再根据经纬度添加真实的Marker。
下面是转换代码:
Point p = baiduMap.getProjection().toScreenLocation(target);
其实核心的就这一句,拿到target(类型为LatLng)的点的经纬度信息后,将它转化为相对于屏幕上的点Point。
Point的x和y属性就是当前相对屏幕的位置,以后你想作什么动画就随便你了,仍是挺简单,是吧。
才接触地图不到2天,有错误的地方请指正,谢谢你们。
这里是Freestyletime@foxmail.com,欢迎交流。
本人原创做品,转载请标明出处。