簡體   English   中英

點擊標記時,是否可以在Google地圖上顯示疊加項?

[英]Is it possible to display an overlay item on Google Maps when a marker is tapped?

我已經制作了一個簡單的android應用程序,該應用程序中還集成了Google Maps。它還能夠連接到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();
    }   
}

看看這個項目- 氣球逐項疊加 它使用自己的類擴展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添加為參數之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM