46

當我從自定義位圖添加標記時,標記未居中指定的位置。居中位圖標記(Google Maps Android API v2)

我加入這樣的:

// ID tramo 
    final int tramoId = tr.getId(); 
    // Nodos 
    final Nodo[] nodos = tr.getNodos(); 

    final PolylineOptions p = new PolylineOptions(); 
    MarkerOptions m = new MarkerOptions(); 
    // Seteamos posicion de linea y marker 
    m.position(semisuma(nodos)); 
    for (final Nodo n : nodos) { 
     p.add(n.getLatLng()); 
    } 
    // Color de linea y marker 
    BitmapDescriptor icon = null; 
    if (tr.getCapacidad() == 0) { 
     p.color(0xFF000000); 
     m = null; 
    } else if (tr.getCapacidad() - tr.getPlazasOcupadas() == 0) { 
     p.color(0xEEFF0000); 
     final TextDrawable drawable = new TextDrawable(0, 0xEEFF0000, 
       0xFFFFFFFF); 
     icon = BitmapDescriptorFactory.fromBitmap(fromDrawable(drawable)); 
    } else { 
     p.color(0xEE00FFFF); 
     final TextDrawable drawable = new TextDrawable(0, 0xEE00FFFF, 
       0xFFFFFFFF); 
     icon = BitmapDescriptorFactory.fromBitmap(fromDrawable(drawable)); 
    } 
    if (m != null) { 
     m.title(String.valueOf(tramoId)); 
     m.icon(icon); 
    } 
    if (polylinesTramo.get(idTramo) != null) { 
     polylinesTramo.get(idTramo).remove(); 
    } 
    if (markersTramo.get(idTramo) != null) { 
     markersTramo.get(idTramo).remove(); 
    } 
    polylinesTramo.put(idTramo, map.getMap().addPolyline(p)); 
    if (marker != null) { 
     markersTramo.put(idTramo, map.getMap().addMarker(m)); 
    } 

這是TextDrawable的代碼:

package com.cidaut.blueparking.ui; 

import android.graphics.Canvas; 
import android.graphics.ColorFilter; 
import android.graphics.Paint; 
import android.graphics.PixelFormat; 
import android.graphics.drawable.Drawable; 

public class TextDrawable extends Drawable { 

    private final String text; 
    private final Paint paint; 
    private final int centroX = 13; 
    private final int centroY = 16; 
    private final float textSize; 
    private final float whereToDrawX; 
    private final int bgColor; 

    public TextDrawable(final int text, final int bgColor, final int textColor) { 
     this(String.valueOf(text), bgColor, textColor); 
    } 

    public TextDrawable(final String text, final int bgColor, final int textColor) { 
     this.text = text; 
     this.bgColor = bgColor; 
     this.paint = new Paint(); 
     paint.setColor(textColor); 
     paint.setTextSize(14f); 
     paint.setAntiAlias(true); 
     paint.setFakeBoldText(true); 
     paint.setStyle(Paint.Style.FILL); 
     paint.setTextAlign(Paint.Align.LEFT); 
     textSize = paint.measureText(text); 
     whereToDrawX = centroX - (textSize/2); 
    } 

    @Override 
    public void draw(final Canvas canvas) { 
     canvas.drawColor(bgColor); 
     canvas.drawText(text, whereToDrawX, centroY, paint); 
    } 

    @Override 
    public void setAlpha(final int alpha) { 
     paint.setAlpha(alpha); 
    } 

    @Override 
    public void setColorFilter(final ColorFilter cf) { 
     paint.setColorFilter(cf); 
    } 

    @Override 
    public int getOpacity() { 
     return PixelFormat.TRANSLUCENT; 
    } 
} 

和這裏的fromDrawable方法

protected Bitmap fromDrawable(final Drawable drawable) { 
    final Bitmap bitmap = Bitmap.createBitmap(25, 25, Config.ARGB_8888); 
    final Canvas canvas = new Canvas(bitmap); 
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 
    drawable.draw(canvas); 
    return bitmap; 
} 

我要補充我的代碼讓標記集中在他們的位置?

回答

132

只需將您的標記錨點設置爲0.5和0.5(圖標中間)即可。

... 
MarkerOptions m = new MarkerOptions(); 
m.anchor(0.5f, 0.5f); 
... 

默認錨定值是(0.5f,1.0f)。 你可以閱讀關於marker here

+1

完美....救了我的日子 –

+1

哦,我的上帝人,你是最好的 –

+0

你是一個拯救生命的人 –

7

如果您使用Marker,則需要重新指定錨點以便將圖像居中。正如文檔所述:「錨點:圖像上的點將放置在標記的LatLng位置,默認爲圖像左側和圖像底部的50%。」

如果您希望圖像在默認情況下居中,您應該考慮使用GroundOverlay而不是標記。請參閱GroundOverlay「錨點默認爲圖像頂部的50%,圖像左側的50%」。

相關問題