【问题标题】:Is it possible to display an overlay item on Google Maps when a marker is tapped?点击标记时是否可以在 Google 地图上显示叠加项目?
【发布时间】:2012-07-04 21:55:29
【问题描述】:

我已经制作了一个简单的安卓应用程序,我还在其中集成了谷歌地图.. 它还能够连接到 MySQL (localhost) 以使用经度和纬度值显示我想要的位置.. 我的问题是,是否可以在单击标记时在 Google 地图上方制作另一个叠加项目(就像在foursquare 中发生的那样)?

具体来说,我想显示一个包含地名的文本。

这是我显示叠加项目的类。 我做了一个 onTap 方法,但是它显示一个对话框,我想显示一个简单的文本框,显示地名。

    package finddroid.map;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
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 CustomItemizedOverlay extends ItemizedOverlay<OverlayItem>
{

    private int markerHeight;

    private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();

    private Context context;

    public CustomItemizedOverlay(Drawable defaultMarker)
    {
        super(boundCenterBottom(defaultMarker));
        markerHeight = ((BitmapDrawable) defaultMarker).getBitmap().getHeight();
        populate();
    }

    public CustomItemizedOverlay(Drawable defaultMarker, Context context)
    {
        this(defaultMarker);
        this.context = context;
    }

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

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

    @Override
    //Event when a place is tapped
    protected boolean onTap(int index)
    {
        OverlayItem item = mapOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(context);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }

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

【问题讨论】:

    标签: java android eclipse google-maps


    【解决方案1】:

    看看这个项目 - balloon itemized overlay。它使用自己的类扩展FrameLayout 来显示气球。

    因此,如果您想修改代码,请将其放入您的 onTap 方法中,以在录音项目上方显示 TextView

    TextView text = new TextView(context);
    text.setText(item.getTitle());
    MapView.LayoutParams params = new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, item.getPoint(), MapView.LayoutParams.BOTTOM_CENTER);
    params.mode = MapView.LayoutParams.MODE_MAP;
    mMapView.addView(text, params);
    

    我认为这段代码简单易懂,您可以根据自己的需要对其进行改进。要使其工作,您必须将 MapView 的实例传递给叠加层的构造函数并将其保存到私有变量 mMapView

    private MapVeiw mMapView;
    
    public CustomItemizedOverlay(Drawable defaultMarker, Context context, MapView mapView) {
        this(defaultMarker);
        this.context = context;
        this.mMapView = mapView;
    }
    

    并且不要忘记在调用new CustomItemizedOverlay() 时添加MapView 作为参数之一。

    【讨论】:

    • 这是一个很好的例子,先生。但我不认为我可以将它插入我的程序。我只是一个 1 个月大的 android 程序员。我会尝试改进我以前的代码。谢谢。
    • 哦,对不起,先生。但我仍然需要你的一点帮助。我不明白“要使其工作,您必须将 MapView 的实例传递给覆盖的构造函数并将其保存到私有变量 mMapView。”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多