百度地图的添加覆盖物marker和InfoWindow总结,自定义标签

本文详细介绍了如何在地图应用中使用自定义视图来绘制图片,并实现地图标记与弹窗之间的交互,包括图片获取、绘制、地图标记创建、点击事件处理以及弹窗展示等关键步骤。

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

//绘制图片的方法

import com.example.headidentify.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
public class MarkXyView{
public static Bitmap getBitmaps(Context context,String title) {
// TODO Auto-generated constructor stub
Bitmap bitmap = Bitmap.createBitmap(80, 90, Config.ARGB_8888);
Bitmap factory = BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_green_new);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setTextSize(30f);
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.STROKE); 
        paint.setAntiAlias(true);//去除锯齿  
        //设置画出的线的 粗细程度  
        paint.setStrokeWidth(3);  
        Paint paint2 = new Paint();
        paint2.setStyle(Paint.Style.STROKE);
        paint2.setAntiAlias(true);
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(factory, 0,0, paint2);
canvas.drawText(title, 30,40, paint);
return bitmap;
}
}

activity 中的调用

MapViewActivity

 LatLng latLng = new LatLng(lan, lng);

OverlayOptions overlayOptions = new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromBitmap(MarkXyView.getBitmaps(MapViewActivity.this,“A")));Marker marker = (Marker) baiduMap.addOverlay(overlayOptions);marker.setTitle("A");

baiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

@Override
public boolean onMarkerClick(Marker arg0) {
SetPopupWindow(listmap.get(Integer.parseInt(arg0.getTitle())).get("NAME"),arg0);

return true;
}
});

//调用的方法

private OnInfoWindowClickListener listener = null;
private void SetPopupWindow(String name,Marker marker) {
View popview = getLayoutInflater().from(this).inflate(R.layout.popupwindow_item, null);
TextView text_name = (TextView)popview.findViewById(R.id.text_name);
Drawable drawable = getResources().getDrawable(R.drawable.ic_red_circle);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
text_name.setCompoundDrawables(drawable, null, null, null);
text_name.setText(name);
listener = new OnInfoWindowClickListener() {
public void onInfoWindowClick() {
//点击时跳转。
Toast.makeText(MapViewActivity.this, "点击事件", 0).show();
}
};
InfoWindow mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(popview), marker.getPosition(), -47, listener);
baiduMap.showInfoWindow(mInfoWindow);
}



注意的是:listener =  new OnInfoWindowClickListener() ;方法,必须放到

InfoWindow mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(popview), marker.getPosition(), -47, listener);
之前,要不然会出现第一次点击没有反应的弹出的信息没有反应的情况



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值