1

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

请帮忙

谢谢你

4

1 回答 1

0

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

我承认,Android API 中的 MapOverlay 比它应该的要复杂一些,但如果你仔细观察,你会很快了解它。我建议从这里开始,因为对我来说未知的原因,Google 将其分离为外部库,并且不再在旧的开发人员页面上可用。仔细看看Itemized Overlay,我用它在地图上绘制对象。您将需要扩展此类并覆盖一些方法。如您所见,它已经有 onTap 方法,您也可以简单地覆盖它:)

为了确保您理解我的意思 - 这是我自己的逐项叠加:

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

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

希望能帮助到你!

于 2012-07-09T14:10:43.483 回答