文本未在MapView Overlay上显示

时间:2011-02-21 19:45:50

标签: android text overlay android-mapview drawable

我可以显示特定区域的地图,其中可绘制的图标显示了确切的位置。问题是我没有看到标记旁边的任何文字,就像你使用谷歌地图一样。它看起来像你发送给OverlayItem的标题或片段文本会显示,但事实并非如此。

我没有在网上找到任何示例,说明如何在我位置的可绘制图标旁边添加某种文字...有没有人有例子?

我最初遵循Google MapView教程,但“Code Magician”有更好的版本: https://codemagician.wordpress.com/2010/05/06/android-google-mapview-tutorial-done-right/

(遗憾的是,地图上的图标旁没有文字标签)

2 个答案:

答案 0 :(得分:27)

我自己发现了这一点......显然这是一个很大的秘密,或者某种东西...... 无论如何,享受代码。高兴地将它复制并粘贴到整个网络中以获取秘密。

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();
    }

}

答案 1 :(得分:1)

地图顶部的内容位于不同的图层上,每个图层只能容纳一个类型的图像,例如只有地图图钉。你需要创建一个单独的 > Overlay用于每种新图像类型(如弹出文本)