0

在谷歌地图 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

4 回答 4

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/

于 2013-10-09T06:24:09.597 回答
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;
}
于 2013-10-11T11:04:52.877 回答
1

为了实现你想要的,你需要创建某种方法来创建你想要BitmapMarker图标形式。这Bitmap将包括您标记的图标以及您要呈现的文本。

你可以看看这个指南来做到这一点:

在位图上绘制文本

接下来,正如已经告知的那样,您需要使用该方法将其提供Bitmap给标记。.setIcon

于 2013-10-09T07:56:37.350 回答
1
mMap.addMarker(new MarkerOptions()
                .icon(youricon)
                .position(point)
                .title("Your location")
                .snippet("Your last recorded location")

        ).showInfoWindow();

标记上的 .showInfoWindow 将显示标题和片段文本,无需用户交互。你可以用同样的方法隐藏它。

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

于 2014-02-18T20:32:12.830 回答