文字不MapView的叠加显示 [英] Text not displaying on MapView Overlay

查看:179
本文介绍了文字不MapView的叠加显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够显示的地图为与可绘制图标表示的确切位置的特定区域。问题是,我没有看到任何文字旁边的标记像你这样做与谷歌地图。它看起来像任你发送到OverlayItem将显示但事实不是这样的标题或文本片段。

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 ?

我原本跟着谷歌MapView的教程,但code魔术师有它的一个更好的版本: <一href="https://$c$cmagician.word$p$pss.com/2010/05/06/android-google-mapview-tutorial-done-right/">https://$c$cmagician.word$p$pss.com/2010/05/06/android-google-mapview-tutorial-done-right/

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)

推荐答案

我发现了这一点,我自己......显然这是一些大的秘密,或东西... 反正,享受code。令人高兴的是复制并粘贴整个网拿到的秘密了。

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

}

这篇关于文字不MapView的叠加显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆