android开发百度地图怎么实现自定义弹出窗口

头像
水果西瓜太郎   2021-09-18   10081浏览
已有60条回答
头像
1982吃货一枚
2022-04-01

基本原理就是用ItemizedOverlay来附加物,在OnTap方法中向MapView上一个自定义的View(如果已存在就直接设为可见),下体来介绍我的实现方法:
  一、自定义覆盖物类:MyPopupOverlay,这个类是最关键的一个类ItemizedOverlay,用于设置Marker,并定义Marker的点击,弹出窗口,至于弹出窗口的内容,则通过定义Listener,放到Activity中去构造。如果没有特殊需求,这个类不需要做什么改动。代如下,popupLinear这个对象,就是加到地图上的自定义View:
  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);
}
}

38

相关问题

android怎么样开发地图定位使用自己重绘的地图?
非非1227 1970-01-01

听人说过安卓下可以开发自己的地图,而不是调用百度 谷歌的地图,请问有谁了解是怎么实现的?

Android地图开发 鼠标移动到不同地点获取不同地点的文字提示信息怎么实现
天道酬勤1212 1970-01-01

地点的坐标或经纬度是从卫星上获取的,文字信息也是从卫星上获取吗,怎么获取?需求很抽象!

最新问答

排水论文在哪发?
伊兰0518 2021-09-19

小区市外排水论文发哪个杂志可以呢?我需要发表一篇这方面的论文。

word转pdf,为什么不显示图片图片?
花花的老妈 2021-09-19

我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!

公众号与小程序有什么区别
汤糖躺烫湯 2021-09-19

公众号与小程序有什么区别

如何制作电子小报
dream959595 2021-09-19

镀铬什么意思
autumngold 2021-09-19

镀铬什么意思

中国电影艺术的思想
幸福顺延 2021-09-19

中国针灸大纲作者是谁?
王子麻麻 2021-09-19

热门问答

排水论文在哪发?
伊兰0518 2021-09-19

小区市外排水论文发哪个杂志可以呢?我需要发表一篇这方面的论文。

word转pdf,为什么不显示图片图片?
花花的老妈 2021-09-19

我想把论文从word格式转换成PDF格式,用的金山WPS,可转换完成之后,里面的流程图就不见了,空白~~这是为什么呢?谁能帮我解决一下!谢谢!

公众号与小程序有什么区别
汤糖躺烫湯 2021-09-19

公众号与小程序有什么区别

如何制作电子小报
dream959595 2021-09-19

镀铬什么意思
autumngold 2021-09-19

镀铬什么意思

中国电影艺术的思想
幸福顺延 2021-09-19

中国针灸大纲作者是谁?
王子麻麻 2021-09-19

Coptyright © www.lw85.com电脑版