Android 选择标记后,如何在标记中应用两个不同的图标?
我使用自定义标记图标来显示。它以绿色显示。点击标记时,如何在标记中应用其他图标。我的代码如下Android 选择标记后,如何在标记中应用两个不同的图标?,android,google-maps-markers,google-maps-android-api-2,Android,Google Maps Markers,Google Maps Android Api 2,我使用自定义标记图标来显示。它以绿色显示。点击标记时,如何在标记中应用其他图标。我的代码如下 Bitmap bmp; bmp=writeTextOnDrawable(context,R.drawable.green,("Hello")); @Override public void onInfoWindowClick(final Marker marker) { // how to change color of marker color/icon whe
Bitmap bmp;
bmp=writeTextOnDrawable(context,R.drawable.green,("Hello"));
@Override
public void onInfoWindowClick(final Marker marker)
{
// how to change color of marker color/icon when it tap.
}
private Bitmap writeTextOnDrawable(Context context,int drawableId, String text) {
Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId)
.copy(Bitmap.Config.ARGB_8888, true);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
Paint paint = new Paint();
paint.setStyle(Style.FILL);
paint.setColor(Color.WHITE);
paint.setTypeface(tf);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(convertToPixels(context, 8));
Rect textRect = new Rect();
paint.getTextBounds(text, 0, text.length(), textRect);
Canvas canvas = new Canvas(bm);
//If the text is bigger than the canvas , reduce the font size
if(textRect.width() >= (canvas.getWidth() - 4)) //the padding on either sides is considered as 4, so as to appropriately fit in the text
paint.setTextSize(convertToPixels(context, 7)); //Scaling needs to be used for different dpi's
//Calculate the positions
int xPos = (canvas.getWidth() / 2) - 2; //-2 is for regulating the x position offset
//"- ((paint.descent() + paint.ascent()) / 2)" is the distance from the baseline to the center.
int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2)) ;
canvas.drawText(text, xPos, yPos, paint);
return bm;
}
private float convertToPixels(Context context, int nDP) {
// TODO Auto-generated method stub
final float conversionScale = context.getResources().getDisplayMetrics().density;
return (int) ((nDP * conversionScale) + 0.5f) ;
}
检查您正在使用的地图API的版本。2013年5月,将方法setIcon()添加到类标记中。(见附件) 因此,您应该能够在OnInfo WindowClick中用一个新图标(具有不同的颜色)替换该图标