Shailendra Madda
Shailendra Madda

Reputation: 21551

Best way to add marker with name on Google Map v2

What is the best way to add marker on Google map v2 with having name bottom of that marker? How to achieve it as like Google maps,is there any API for this? I tried it working fine but not in all devices perfectly.(showing marker name on top of the marker and on the marker in some devices).

// 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;
}

Upvotes: 0

Views: 7120

Answers (4)

John
John

Reputation: 7826

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

        ).showInfoWindow();

The .showInfoWindow on the Marker will show the Title and Snippet text without user interaction. You can hide it the same way.

Without .showInfoWindow it will only show the information when clicking it.

Upvotes: 1

Shailendra Madda
Shailendra Madda

Reputation: 21551

This is what i am expected:

// 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;
}

Upvotes: 2

Emil Adz
Emil Adz

Reputation: 41099

To achive what you want you will need to create some kind of method that will create your desired Bitmap form the Marker icon. this Bitmap will include you marker's icon with the text you want to present.

You can take a look at this guide to do that:

Draw Text on Bitmap

Next as already been told, you will need to provide this Bitmap to the marker using the .setIcon method.

Upvotes: 1

Oleg Belousov
Oleg Belousov

Reputation: 10121

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

mMap should be of type GoogleMap of course.

If you need anything additional just comment

Alternatively, you can set the icon image using the Marker.setIcon() method.

For more information, you can refer to this tutorial: http://www.androidhive.info/2013/08/android-working-with-google-maps-v2/

Upvotes: 3

Related Questions