如何添加上述谷歌地图标记的文字? [英] How to add text above a marker on Google Maps?

查看:165
本文介绍了如何添加上述谷歌地图标记的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 尝试
    {
            JSONArray jArray =新JSONArray(结果);
            的for(int i = 0; I< jArray.length();我++)
            {
                    的JSONObject json_data = jArray.getJSONObject(ⅰ);
                    //从数据库中检索数据。纬度和放大器;长=双/名称=字符串
                    latMarker = json_data.getDouble(纬);
                    lonMarker = json_data.getDouble(经度);
                    nameMarker = json_data.getString(姓名);
                    名单<覆盖> mapOverlays =调用MapView.getOverlays();
                    可绘制绘制= this.getResources()getDrawable(R.drawable.ic_launcher)。
                    CustomItemizedOverlay itemizedOverlay =
                         新CustomItemizedOverlay(绘制,这一点);

                    标记=新的GeoPoint(
                            (INT)(latMarker * 1E6)
                            (中间体)(lonMarker * 1E6));

                    //偏偏标记被点击时
                    OverlayItem overlayitem =新OverlayItem(标记,抽头,姓名:+ nameMarker);
                    itemizedOverlay.addOverlay(overlayitem);
                    mapOverlays.add(itemizedOverlay);
            }
    }
    赶上(JSONException E)
    {
            Log.e(log_tag+ e.toString()错误分析数据。);
    }
}
 

我做了一个Android应用程序,并集成谷歌地图吧。我还添加了标记上的位置在我的城市的地方。而我的问题是,我不知道如何添加上面这些标记文本。谁能帮帮我,好吗?

解决方案

 包com.labs;

进口的java.util.ArrayList;

进口android.graphics.Paint;
进口android.graphics.Point;
进口android.graphics.Rect;
进口android.graphics.RectF;
进口android.graphics.drawable.BitmapDrawable;
进口android.graphics.drawable.Drawable;
进口android.text.TextPaint;

进口com.google.android.maps.GeoPoint;
进口com.google.android.maps.ItemizedOverlay;
进口com.google.android.maps.MapView;
进口com.google.android.maps.OverlayItem;

公共类ResoucesOverlay扩展ItemizedOverlay
{

    公共ResoucesOverlay(可绘制defaultMarker)
    {
        / *对defaultMarker调用boundCenterBottom(),使标记图像连接
          在其底部中心,以这种覆盖物品的纬度和经度* /
        超(boundCenterBottom(defaultMarker));

        markerHeight =((BitmapDrawable)defaultMarker).getBitmap()的getHeight()。

        / *这个调用来填充是非常重要的。尽管这不会出现在图形页面教程
         *谷歌Android开发者网站,的MapView有时崩溃没有这个电话。
         * /
        填充();
    }

    @覆盖
    保护OverlayItem createItem中(int i)以
    {
        返回mOverlays.get(ⅰ);
    }

    @覆盖
    公众诠释大小()
    {
        返回mOverlays.size();
    }

    @覆盖
    公共无效画(android.graphics.Canvas帆布,图形页面图形页面,
            布尔影子)
    {
        super.draw(帆布,图形页面,阴影);

        //通过所有OverlayItems并绘制标题为他们每个人
        对于(OverlayItem项目:mOverlays)
        {
            / *将转换纬度和放大器;该项目覆盖的经度坐标在屏幕上。
             *由于我们称之为boundCenterBottom()的构造函数,所以这些坐标
             *将显示标记的底部中心位置。
             * /
            的GeoPoint点= item.getPoint();
            点markerBottomCenterCoords =新的点();
            MapView.getProjection()在toPixels(点,markerBottomCenterCoords)。

            / *查找标题的宽度和高度* /
            TextPaint paintText =新TextPaint();
            油漆paintRect =新的油漆();

            矩形RECT =新的矩形();
            paintText.setTextSize(FONT_SIZE);
            paintText.getTextBounds(item.getTitle(),0,item.getTitle()长度(),正确。);

            rect.inset(-TITLE_MARGIN,-TITLE_MARGIN);
            rect.offsetTo(markerBottomCenterCoords.x  -  rect.width()/ 2,markerBottomCenterCoords.y  -  markerHeight  -  rect.height());

            paintText.setTextAlign(Paint.Align.CENTER);
            paintText.setTextSize(FONT_SIZE);
            paintText.setARGB(255,255,255,255);
            paintRect.setARGB(130,0,0,0);

            canvas.drawRoundRect(新RectF(矩形),2,2,paintRect);
            canvas.drawText(item.getTitle(),rect.left + rect.width()/ 2,
                    rect.bottom  -  TITLE_MARGIN,paintText);
        }
    }

    公共无效addOverlay(INT纬度,经度INT,字符串称号,
            字符串片段)
    {
        OverlayItem项目;

        GeoPoint对象的GeoPoint =新的GeoPoint(纬度,经度);
        项目=新OverlayItem(GeoPoint对象,标题段);
        mOverlays.add(项目);
        填充();

    }

    公共无效addOverlay(OverlayItem overlayItem)
    {
        mOverlays.add(overlayItem);
        填充();
    }

    私人诠释markerHeight;
    私人的ArrayList< OverlayItem> mOverlays =新的ArrayList< OverlayItem>();

    私有静态最终诠释FONT_SIZE = 12;
    私有静态最终诠释TITLE_MARGIN = 3;
}
 

    try
    {
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++)
            {
                    JSONObject json_data = jArray.getJSONObject(i);                       
                    //Retrieve data from db. lat & long = double / name = String
                    latMarker = json_data.getDouble("latitude");
                    lonMarker = json_data.getDouble("longitude");
                    nameMarker = json_data.getString("name");
                    List<Overlay> mapOverlays = mapView.getOverlays();
                    Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher);
                    CustomItemizedOverlay itemizedOverlay = 
                         new CustomItemizedOverlay(drawable, this);

                    markers = new GeoPoint (
                            (int) (latMarker * 1E6),
                            (int) (lonMarker * 1E6));

                    //Happens when a marker is clicked
                    OverlayItem overlayitem = new OverlayItem(markers, "Tapped", "Name: " + nameMarker);
                    itemizedOverlay.addOverlay(overlayitem);
                    mapOverlays.add(itemizedOverlay);           
            }
    }
    catch(JSONException e)
    {
            Log.e("log_tag", "Error parsing data. " + e.toString());
    }        
}

I made an android application and integrated Google Maps to it. I also added markers on places that are located around my city. And my problem is, I dont know how to add text above these markers. Can anyone help me, please?

解决方案

package com.labs;

import java.util.ArrayList;

import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextPaint;

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 ResoucesOverlay extends ItemizedOverlay
{

    public ResoucesOverlay(Drawable defaultMarker)
    {
        /*Calling boundCenterBottom() on defaultMarker makes the marker image connect
          at its Bottom center to the latitude and longitude of this Overlay Item*/
        super(boundCenterBottom(defaultMarker));

        markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();

        /* This call to populate is important. Although this does not appear in the MapView tutorial
         * on Google's Android developer site, the mapview some times crashes without this call.
         */
        populate();
    }

    @Override
    protected OverlayItem createItem(int i)
    {
        return mOverlays.get(i);
    }

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

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

        // go through all OverlayItems and draw title for each of them
        for (OverlayItem item:mOverlays)
        {
            /* Converts latitude & longitude of this overlay item to coordinates on screen.
             * As we have called boundCenterBottom() in constructor, so these coordinates
             * will be of the bottom center position of the displayed marker.
             */
            GeoPoint point = item.getPoint();
            Point markerBottomCenterCoords = new Point();
            mapView.getProjection().toPixels(point, markerBottomCenterCoords);

            /* Find the width and height of the title*/
            TextPaint paintText = new TextPaint();
            Paint paintRect = new Paint();

            Rect rect = new Rect();
            paintText.setTextSize(FONT_SIZE);
            paintText.getTextBounds(item.getTitle(), 0, item.getTitle().length(), rect);

            rect.inset(-TITLE_MARGIN, -TITLE_MARGIN);
            rect.offsetTo(markerBottomCenterCoords.x - rect.width()/2, markerBottomCenterCoords.y - markerHeight - rect.height());

            paintText.setTextAlign(Paint.Align.CENTER);
            paintText.setTextSize(FONT_SIZE);
            paintText.setARGB(255, 255, 255, 255);
            paintRect.setARGB(130, 0, 0, 0);

            canvas.drawRoundRect( new RectF(rect), 2, 2, paintRect);
            canvas.drawText(item.getTitle(), rect.left + rect.width() / 2,
                    rect.bottom - TITLE_MARGIN, paintText);
        }
    }

    public void addOverlay(int latitude, int longitude, String title,
            String snippet)
    {
        OverlayItem item;

        GeoPoint geopoint = new GeoPoint(latitude, longitude);
        item = new OverlayItem(geopoint, title, snippet);
        mOverlays.add(item);
        populate();

    }

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

    private int markerHeight;
    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

    private static final int FONT_SIZE = 12;
    private static final int TITLE_MARGIN = 3;
}

这篇关于如何添加上述谷歌地图标记的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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