Home » Android » android – Text not displaying on MapView Overlay

android – Text not displaying on MapView Overlay

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am able to display a map for a specific area with the drawable icon showing the exact location. The problem is I don’t see any text next to the marker like you do with google maps. It looked like either the title or snippet text you send to the OverlayItem would display but that is not the case.

I haven’t found any example online that shows how to put some kind of text next to the drawable icon at my location… does anyone have an example ?

I originally followed the Google MapView tutorial, but “Code Magician” has a much better version of it:
https://codemagician.wordpress.com/2010/05/06/android-google-mapview-tutorial-done-right/

(Unfortunately without a text label next to the icon on the map)

How to&Answers:

I found this out by myself… apparently it’s some big secret, or something…
anyway, enjoy the code. Happily copy and paste it throughout the ‘net to get the secret out.

package myjunk.android.gps;

import java.util.List;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ActivityMap extends MapActivity
{
    //member variables
    private static final String TAG = "MyMapActivity";
    private MapView mapView;
    private MapController mapController;
    private float mFloatMyLat;
    private float mFloatMyLon;
    private float mFloatOtherPersonLat;
    private float mFloatOtherPersonLon;

    private List<Overlay> mapOverlays;
    private Drawable drawable;
    private MapItemizedOverlay itemizedOverlay;


    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        mapView = (MapView) findViewById(R.id.map);

        mapController = mapView.getController();

        //zoom to street level showing a few blocks
        //1 is world view, 21 is the tightest zoom possible
        mapController.setZoom(17);
        //mapView.setSatellite(true);//show satellite view
        mapView.setStreetView(false);//setting this to true causes blue lines around roads
        mapView.invalidate();

        mapOverlays = mapView.getOverlays();
        drawable = this.getResources().getDrawable(R.drawable.androidmarker);

        Bundle extras = getIntent().getExtras();
        if (extras != null)
        {
            //Get starting locations passed in by previous Activity
            mStrNickname = extras.getString("NickName");
            mFloatMyLat = extras.getFloat("MyLat");
            mFloatMyLon = extras.getFloat("MyLon");
            mFloatOtherPersonLat = extras.getFloat("OthersLat");
            mFloatOtherPersonLon = extras.getFloat("OthersLon");
        }


        itemizedOverlay = new MapItemizedOverlay(drawable, this, 30);//text size: 30

        //put my location on the map
        GeoPoint gPointMe = new GeoPoint((int)(mFloatMyLat  * 1000000),(int)(mFloatMyLon  * 1000000));
        OverlayItem overlayItem = new OverlayItem(gPointMe, "Me", "This is my location");
        itemizedOverlay.addOverlay(overlayItem);

        //show other person on map
        GeoPoint gPoint = new GeoPoint((int)(mFloatOtherPersonLat  * 1000000),(int)(mFloatOtherPersonLon  * 1000000));
        overlayItem = new OverlayItem(gPoint, mStrNickname, "This is that person's location");
        itemizedOverlay.addOverlay(overlayItem);

        //add overlay items to master list of overlays
        mapOverlays.add(itemizedOverlay);
        mapView.invalidate();

        mapView.setBuiltInZoomControls(true);

        //move map over to my position
        mapController.animateTo(gPointMe);
    }

    @Override
    protected boolean isRouteDisplayed()
    {
        return false;
    }
}





package myjunk.android.gps;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;


public class MapItemizedOverlay extends ItemizedOverlay<OverlayItem>
{
    //member variables
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    private int mTextSize;


    public MapItemizedOverlay(Drawable defaultMarker, Context context, int textSize)
    {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
        mTextSize = textSize;
    }


    //In order for the populate() method to read each OverlayItem, it will make a request to createItem(int)
    // define this method to properly read from our ArrayList
    @Override
    protected OverlayItem createItem(int i)
    {
        return mOverlays.get(i);
    }


    @Override
    public int size()
    {
        return mOverlays.size();
    }

    @Override
    protected boolean onTap(int index)
    {
        OverlayItem item = mOverlays.get(index);

        //Do stuff here when you tap, i.e. :
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();

        //return true to indicate we've taken care of it
        return true;
    }

    @Override
    public void draw(android.graphics.Canvas canvas, MapView mapView, boolean shadow)
    {
        super.draw(canvas, mapView, shadow);

        if (shadow == false)
        {
            //cycle through all overlays
            for (int index = 0; index < mOverlays.size(); index++)
            {
                OverlayItem item = mOverlays.get(index);

                // Converts lat/lng-Point to coordinates on the screen
                GeoPoint point = item.getPoint();
                Point ptScreenCoord = new Point() ;
                mapView.getProjection().toPixels(point, ptScreenCoord);

                //Paint
                Paint paint = new Paint();
                paint.setTextAlign(Paint.Align.CENTER);
                paint.setTextSize(mTextSize);
                paint.setARGB(150, 0, 0, 0); // alpha, r, g, b (Black, semi see-through)

                //show text to the right of the icon
                canvas.drawText(item.getTitle(), ptScreenCoord.x, ptScreenCoord.y+mTextSize, paint);
            }
        }
    }


    public void addOverlay(OverlayItem overlay)
    {
        mOverlays.add(overlay);
        populate();
    }


    public void removeOverlay(OverlayItem overlay)
    {
        mOverlays.remove(overlay);
        populate();
    }


    public void clear()
    {
        mOverlays.clear();
        populate();
    }

}

Answer:

The stuff on top of the maps are on different layers, and each layer can hold only one type of images, for example only map pins. You need to create a separate
> Overlay for each new image type (like popup text)