【问题标题】:Text not displaying on MapView Overlay文本未显示在 MapView 叠加层上
【发布时间】:2011-02-21 19:45:50
【问题描述】:

我可以显示特定区域的地图,其中可绘制图标显示确切位置。问题是我看不到标记旁边的任何文字,就像您使用谷歌地图所做的那样。看起来您发送到 OverlayItem 的标题或 sn-p 文本会显示,但事实并非如此。

我没有在网上找到任何示例来说明如何在我所在位置的可绘制图标旁边放置某种文本...有人有示例吗?

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

(很遗憾地图上的图标旁边没有文字标签)

【问题讨论】:

    标签: android text overlay android-mapview drawable


    【解决方案1】:

    这是我自己发现的……显然这是什么大秘密,或者什么…… 无论如何,享受代码。愉快地将其复制并粘贴到整个网络中以找出秘密。

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

    【讨论】:

    【解决方案2】:

    地图上的东西在不同的图层上,每个图层只能保存一种类型的图像,例如只有地图图钉。您需要创建一个单独的 > Overlay 用于每种新的图像类型(如弹出文本)

    【讨论】:

    • 对,这就是我的印象。但是我现在需要的是一个代码示例......我真的没想到使用地图会这么难
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多