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

//绘制图片的方法

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);
之前,要不然会出现第一次点击没有反应的弹出的信息没有反应的情况

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


你可能感兴趣的:(百度地图的添加覆盖物marker和InfoWindow总结,自定义标签)