android 底部弹出popp,android实现百度地图自定义弹出窗口功能

这个博客主要讲解如何在Android中自定义地图上的PopupOverlay,实现点击Marker时弹出窗口,并允许用户自定义窗口内容。同时,介绍了如何设置默认Marker图标,并在点击地图其他区域时关闭弹窗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class MyPopupOverlay extends ItemizedOverlay {

private Context context = null;

// 这是弹出窗口, 包括内容部分还有下面那个小三角

private LinearLayout popupLinear = null;

// 这是弹出窗口的内容部分

private View popupView = null;

private MapView mapView = null;

private Projection projection = null;

// 这是弹出窗口内容部分使用的layoutId,在Activity中设置

private int layoutId = 0;

// 是否使用百度带有A-J字样的Marker

private boolean useDefaultMarker = false;

private int[] defaultMarkerIds = { R.drawable.icon_marka,

R.drawable.icon_markb, R.drawable.icon_markc,

R.drawable.icon_markd, R.drawable.icon_marke,

R.drawable.icon_markf, R.drawable.icon_markg,

R.drawable.icon_markh, R.drawable.icon_marki,

R.drawable.icon_markj, };

// 这个Listener用于在Marker被点击时让Activity填充PopupView的内容

private OnTapListener onTapListener = null;

public MyPopupOverlay(Context context, Drawable marker, MapView mMapView) {

super(marker, mMapView);

this.context = context;

this.popupLinear = new LinearLayout(context);

this.mapView = mMapView;

popupLinear.setOrientation(LinearLayout.VERTICAL);

popupLinear.setVisibility(View.GONE);

projection = mapView.getProjection();

}

@Override

public boolean onTap(GeoPoint pt, MapView mMapView) {

// 点击窗口以外的区域时,当前窗口关闭

if (popupLinear != null && popupLinear.getVisibility() == View.VISIBLE) {

LayoutParams lp = (LayoutParams) popupLinear.getLayoutParams();

Point tapP = new Point();

projection.toPixels(pt, tapP);

Point popP = new Point();

projection.toPixels(lp.point, popP);

int xMin = popP.x - lp.width / 2 + lp.x;

int yMin = popP.y - lp.height + lp.y;

int xMax = popP.x + lp.width / 2 + lp.x;

int yMax = popP.y + lp.y;

if (tapP.x < xMin || tapP.y < yMin || tapP.x > xMax

|| tapP.y > yMax)

popupLinear.setVisibility(View.GONE);

}

return false;

}

@Override

protected boolean onTap(int i) {

// 点击Marker时,该Marker滑动到地图中央偏下的位置,并显示Popup窗口

OverlayItem item = getItem(i);

if (popupView == null) {

// 如果popupView还没有创建,则构造popupLinear

if (!createPopupView()){

return true;

}

}

if (onTapListener == null)

return true;

popupLinear.setVisibility(View.VISIBLE);

onTapListener.onTap(i, popupView);

popupLinear.measure(0, 0);

int viewWidth = popupLinear.getMeasuredWidth();

int viewHeight = popupLinear.getMeasuredHeight();

LayoutParams layoutParams = new LayoutParams(viewWidth, viewHeight,

item.getPoint(), 0, -60, LayoutParams.BOTTOM_CENTER);

layoutParams.mode = LayoutParams.MODE_MAP;

popupLinear.setLayoutParams(layoutParams);

Point p = new Point();

projection.toPixels(item.getPoint(), p);

p.y = p.y - viewHeight / 2;

GeoPoint point = projection.fromPixels(p.x, p.y);

mapView.getController().animateTo(point);

return true;

}

private boolean createPopupView() {

// TODO Auto-generated method stub

if (layoutId == 0)

return false;

popupView = LayoutInflater.from(context).inflate(layoutId, null);

popupView.setBackgroundResource(R.drawable.popupborder);

ImageView dialogStyle = new ImageView(context);

dialogStyle.setImageDrawable(context.getResources().getDrawable(

R.drawable.iw_tail));

popupLinear.addView(popupView);

android.widget.LinearLayout.LayoutParams lp = new android.widget.LinearLayout.LayoutParams(

LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

lp.topMargin = -2;

lp.leftMargin = 60;

popupLinear.addView(dialogStyle, lp);

mapView.addView(popupLinear);

return true;

}

@Override

public void addItem(List items) {

// TODO Auto-generated method stub

int startIndex = getAllItem().size();

for (OverlayItem item : items){

if (startIndex >= defaultMarkerIds.length)

startIndex = defaultMarkerIds.length - 1;

if (useDefaultMarker && item.getMarker() == null){

item.setMarker(context.getResources().getDrawable(

defaultMarkerIds[startIndex++]));

}

}

super.addItem(items);

}

@Override

public void addItem(OverlayItem item) {

// TODO Auto-generated method stub

// 重载这两个addItem方法,主要用于设置自己默认的Marker

int index = getAllItem().size();

if (index >= defaultMarkerIds.length)

index = defaultMarkerIds.length - 1;

if (useDefaultMarker && item.getMarker() == null){

item.setMarker(context.getResources().getDrawable(

defaultMarkerIds[getAllItem().size()]));

}

super.addItem(item);

}

public void setLayoutId(int layoutId) {

this.layoutId = layoutId;

}

public void setUseDefaultMarker(boolean useDefaultMarker) {

this.useDefaultMarker = useDefaultMarker;

}

public void setOnTapListener(OnTapListener onTapListener) {

this.onTapListener = onTapListener;

}

public interface OnTapListener {

public void onTap(int index, View popupView);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值