在Google Map v2上添加带名称标记的最佳方式

时间:2013-10-09 06:20:20

标签: android google-maps-markers google-maps-android-api-2

在Google地图v2上使用该标记的名称底部添加标记的最佳方法是什么?如何像谷歌地图一样实现它,是否有任何API?我试过它工作正常,但并不是在所有设备上完美。(在标记的顶部和某些设备的标记上显示标记名称。)

// To put marker name and icon on the map
private Bitmap drawTitleOnMarkerIcon(MarkingInfo markingInfo, int color) {
    String title = markingInfo.getName();

    int icon = AddLocationTypeIcon.getIcon(markingInfo.getType());
    Bitmap bm = BitmapFactory.decodeResource(getResources(), icon).copy(Bitmap.Config.ARGB_8888, true);

    int bmWidth = bm.getWidth();
    int bmHeight = bm.getHeight();

    int canvasWidth = bmWidth;
    int canvasHeight = bmHeight;
    if (bmWidth > 90) {
        canvasWidth = (int)convertToPixels(getApplicationContext(), bmWidth - bmWidth/2);
        canvasHeight = (int)convertToPixels(getApplicationContext(), bmHeight - bmHeight/2);
        bm = Bitmap.createScaledBitmap(bm, canvasWidth, canvasHeight, true);
    } else {
        canvasWidth = (int)convertToPixels(getApplicationContext(), 72);
        canvasHeight = (int)convertToPixels(getApplicationContext(), 72);
        bm = Bitmap.createScaledBitmap(bm, canvasWidth, canvasHeight, true);
    }

    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAntiAlias(true);
    paint.setFakeBoldText(true);
    paint.setTextSize(convertToPixels(getApplicationContext(), paint.getTextSize()-3));

    float canvasCoorHeight = canvasHeight - paint.getTextSize()*2;// first y coordinate to start text from 
    while(title.length() > 0) {
        title = drawTextOnCanvas(paint, bm, title, canvasWidth, canvasCoorHeight);
        canvasCoorHeight = canvasCoorHeight + paint.getTextSize();// next text to draw at 12.5 + height
    }

    return bm;
}

4 个答案:

答案 0 :(得分:3)

mMap.addMarker(new MarkerOptions()
    .position(new LatLng(lati, longi))
    .title("Hello world"))
    .icon(BitmapDescriptorFactory.fromResource(userIcon))
    .snippet("Your last recorded location")

mMap当然应该是GoogleMap类型。

如果您还需要其他任何评论

或者,您可以使用Marker.setIcon()方法设置图标图像。

有关更多信息,请参阅本教程: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

答案 1 :(得分:2)

这就是我所期待的:

// To show the map with existing user created markers
private void addExistingMarkers(int color) {
    userMarkers.clear();// remove all existing markers
    /**
     * Placing Marker
     * */
    List<MarkingInfo> markingInfos = SQLLiteManager.getInstance(getApplicationContext())
            .getAllAppMarkingInfos();

    for (MarkingInfo markingInfo : markingInfos) {

        // Set title on marker
        //Bitmap drawBmp = writeTextOnDrawable(markingInfo, color);
        Bitmap drawBmp = drawTitleOnMarkerIcon(markingInfo, color);


        MarkerOptions markerOptions = new MarkerOptions()
                .position(new LatLng(markingInfo.getLattitude(), markingInfo.getLongtitutde()))
                .title(markingInfo.getName())
                .icon(BitmapDescriptorFactory.fromBitmap(drawBmp))
                .icon(BitmapDescriptorFactory.fromBitmap(drawBmp))
                // .anchor(0.5f, 1)
                .draggable(true)
                .visible(false);

        if (googleMap.getCameraPosition().zoom >= markingInfo.getZoomlevel()) {
            markerOptions.visible(true);
        }

        Marker marker = googleMap.addMarker(markerOptions);
        userMarkers.put(marker, markingInfo);
        marker.setDraggable(false);
    }
    Log.i("Existing markers", "Successfully placed existing markes on map ");
}

// To put marker name and icon on the map
private Bitmap drawTitleOnMarkerIcon(MarkingInfo markingInfo, int color) {
    String title = markingInfo.getName();

    int icon = AddLocationTypeIcon.getIcon(markingInfo.getType());
    Bitmap bm = BitmapFactory.decodeResource(getResources(), icon).copy(Bitmap.Config.ARGB_8888, true);

    int bmWidth = bm.getWidth();
    int bmHeight = bm.getHeight();

    int canvasWidth = bmWidth;
    int canvasHeight = bmHeight;
    if (bmWidth > 90) {
        canvasWidth = (int)convertToPixels(getApplicationContext(), bmWidth - bmWidth/2);
        canvasHeight = (int)convertToPixels(getApplicationContext(), bmHeight - bmHeight/2);
        bm = Bitmap.createScaledBitmap(bm, canvasWidth, canvasHeight, true);
    } else {
        canvasWidth = (int)convertToPixels(getApplicationContext(), 72);
        canvasHeight = (int)convertToPixels(getApplicationContext(), 72);
        bm = Bitmap.createScaledBitmap(bm, canvasWidth, canvasHeight, true);
    }

    Paint paint = new Paint();
    paint.setColor(color);
    paint.setAntiAlias(true);
    paint.setFakeBoldText(true);
    paint.setTextSize(convertToPixels(getApplicationContext(), paint.getTextSize()-3));

    float canvasCoorHeight = canvasHeight/2 + paint.getTextSize() + 6;// first y coordinate to start text from

    while(title.length() > 0) {
        title = drawTextOnCanvas(paint, bm, title, canvasWidth, canvasCoorHeight);
        canvasCoorHeight = canvasCoorHeight + paint.getTextSize();// next text to draw at 12.5 + height
    }

    return bm;
}

答案 2 :(得分:1)

要获得您想要的内容,您需要创建某种方法,以便在Bitmap图标中创建所需的Marker。此Bitmap将包含标记的图标以及您要显示的文字。

您可以查看本指南来执行此操作:

Draw Text on Bitmap

接下来已经告知过,您需要使用Bitmap方法将此.setIcon提供给标记。

答案 3 :(得分:1)

mMap.addMarker(new MarkerOptions()
                .icon(youricon)
                .position(point)
                .title("Your location")
                .snippet("Your last recorded location")

        ).showInfoWindow();

Marker上的.showInfoWindow将显示Title和Snippet文本而无需用户交互。 你可以用同样的方式隐藏它。

如果没有.showInfoWindow,它只会在点击时显示信息。

相关问题