1、
private PopupOverlay pop = null; /** * pop弹窗事件监控接口 */ PopupClickListener popListener = new PopupClickListener(){ @Override public void onClickedPopup(int index) { //index - 当前点击的区域索引。 if ( index == 0){ //更新item位置 pop.hidePop(); GeoPoint p = new GeoPoint(mCurItem.getPoint().getLatitudeE6()+5000, mCurItem.getPoint().getLongitudeE6()+5000); mCurItem.setGeoPoint(p); mOverlay.updateItem(mCurItem); mMapView.refresh(); } else if(index == 2){ //更新图标 mCurItem.setMarker(getResources().getDrawable(R.drawable.nav_turn_via_1)); mOverlay.updateItem(mCurItem); mMapView.refresh(); } } }; //PopupOverlay构造器 pop = new PopupOverlay(mMapView,popListener);
2、显示
pop.showPopup(View view, GeoPoint point, int yOffset); //view:自定义 View //point:弹窗的位置(锚点在窗口的中下方向。水平方向:Center;垂直方向:Bottom) //yOffset:弹窗在y轴上的偏移量,取值范围yOffset>=0。单位:像素
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro>
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/popleft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/popup_side"
android:gravity="center"
android:textStyle="bold"
android:textColor="#3814ed"
android:text="交通顺畅"
android:textSize="12sp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/popinfo"
>
<TextView
android:id="@+id/textcache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/popup_middle"
android:gravity="center"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textSize="12sp" />
<TextView
android:id="@+id/popdown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/popup_down"
android:textColor="@android:color/black"
android:textSize="12sp" />
</LinearLayout>
<TextView
android:id="@+id/popright"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/popup_side"
android:textColor="#3814ed"
android:gravity="center"
android:textStyle="bold"
android:text="指点路况"
android:textSize="12sp" />
</LinearLayout>
构造view
private View viewCache; /** * 向地图添加自定义View. */ viewCache = getLayoutInflater() .inflate(R.layout.custom_text_view, null); popupInfo = (View) viewCache.findViewById(R.id.popinfo); popupLeft = (View) viewCache.findViewById(R.id.popleft); popupRight = (View) viewCache.findViewById(R.id.popright); popupText = (TextView) viewCache.findViewById(R.id.textcache); popupText.setText("南湖大道"); Bitmap[] bitMaps = { BMapUtil.getBitmapFromView(popupLeft), BMapUtil.getBitmapFromView(popupInfo), BMapUtil.getBitmapFromView(popupRight) }; pop.showPopup(bitMaps, geoPoint, 32);
3、
//收起pop弹窗 if (pop != null){ pop.hidePop(); }
4、从view生成bitmap
public class BMapUtil { /** * 从view 得到图片 * @param view * @return */ public static Bitmap getBitmapFromView(View view) { view.destroyDrawingCache(); view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.setDrawingCacheEnabled(true); Bitmap bitmap = view.getDrawingCache(true); return bitmap; } }
Done!