在android中获取自定义地图标记的界限

时间:2012-07-09 05:34:15

标签: android google-maps google-maps-markers google-maps-mobile

我的地图视图上有一组标记。我想得到一个地图标记的边界,以便我可以检测到我点击的是哪个地图标记。我在stackoverflow中搜索但是所有人都在告诉如何设置地图标记的边界(drawable)。但我想要的是获得我已经绘制的标记的边界。

请帮忙

谢谢

1 个答案:

答案 0 :(得分:0)

This link导致项目我建议不要复制,但要从中获取灵感。看看哪些方法用于什么,也许你可以在那里找到一些好主意。我不建议您出于自己的原因进行复制 - 在编程时学习(如果您只是复制,则不会学到任何东西)。

我承认,MapOverlay在Android API中要比应有的复杂得多,但如果你仔细看看,你会很快进入它。我建议启动here,因为我不明原因Google将其分为外部库,旧开发者页面上不再提供。仔细查看Itemized Overlay,我用它来绘制地图上的对象。您将需要扩展此类并覆盖几个方法。正如您所看到的,它已经有了onTap方法,您只需覆盖它:)

为了确保你理解我的意思 - 这是我自己的Itemized Overlay:

package example;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources.NotFoundException;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
/**
 * Paints and stores all the Overlays for CurrentPositionActivity, overrides ItemizedOverlay<OvelayItem>.
 * @author Michal Svacha
 * 
 */
 public class ItemizedOverlayBeta extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); // ArrayList for the OverlayItems
Context mContext; // Context where the Overlays are to eb displayed
private ArrayList<String> mObjIDs = new ArrayList<String>();
DetailLoader dl;

public void addID(String objId) {
    mObjIDs.add(objId);
}

/**
 * Public constructor.
 * @param defaultMarker
 * @param context
 */
public ItemizedOverlayBeta(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker)); // Calls the constructor of the parent.
    mContext = context;
}

/**
 * Adds overlay to ArrayList.
 * @param overlay
 */
public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

/**
 * Returns OverlayItem based on the index.
 * @param index - position where the OverlayItem is stored in the ArrayList.
 * @return desired OverlayItem.
 */
@Override
protected OverlayItem createItem(int index) {
  return mOverlays.get(index);
}

/**
 * Returns the size of the ArrayList
 * @return calls method size() on the ArrayList
 */
@Override
public int size() {
  return mOverlays.size();
}

/**
 * When tapped on screen this method is called and creates AlertDialog with little data.
 * @param index - place where tapped
 * @return always true
 */
@Override
protected boolean onTap(int index) {
  dl = new DetailLoader();
  try {
    if(dl.execute(index).get()) {
        OverlayItem item = mOverlays.get(index);
        final AlertDialog dialog = new AlertDialog.Builder(mContext).create();
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet() +"\n" + "Hello");
        dialog.setButton("close", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
      });
      dialog.show();
      }
} catch (NotFoundException e) {
    return false;
} catch (InterruptedException e) {
    return false;
} catch (ExecutionException e) {
    return false;
}
  return true;
}

public void populateNow(){
    populate();
}

private class DetailLoader extends AsyncTask<Integer, Void, Boolean>{
    ProgressDialog pd;

    @Override
    protected void onPreExecute() {
        pd = new ProgressDialog(mContext);
        pd = ProgressDialog.show(mContext, "","Loading",true);
    }

    @Override
    protected void onPostExecute(Boolean result) {
        if(pd != null) pd.dismiss();
    }

    @Override
    protected Boolean doInBackground(Integer... params) {
        // Load data
        return true;
    }
}
}

然后在地图上显示如下:

ItemizedOverlayBeta itemizedMoving = new ItemizedOverlayBeta(this.getResources().getDrawable(R.drawable.ic_pin_moving), this);
GeoPoint gp = new GeoPoint((int)(coordinate * 1E6),(int)(coordinate2 * 1E6));
OverlayItem oi = new OverlayItem(gp, "string for the alert dialog";    
itemizedMoving.populateNow();
CurrentPositionActivity.mapOverlays.add(itemizedMoving);

所以基本上当你在引脚上需要一些弹出窗口时,你需要在叠加层中实现它,而不是地图,因为,你告诉引脚 - 嘿,当我点击你时,你需要重绘自己,就像我一样我正在显示警告对话框。

希望它有所帮助!