1

所以我只是想在 android 中创建一个通用的 mapView 并在其中放置一些地理点。我已按照 android 开发者网站上的说明进行操作,但没有成功……有人可以帮忙吗?这是我的代码。

public class HelloItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

    public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
        // defines the bounds for the overlayItems
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    @Override
    protected OverlayItem createItem(int i) {
        // returns the correct ArrayList position from int i
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        // returns number of items in ArrayList
        return mOverlays.size();
    }

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

    @Override
    protected boolean onTap(int index) {
        OverlayItem item = mOverlays.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());
        dialog.show();
        return true;
    }

}

//这是我的主要课程

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maptastic);
        ((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
        setTitle("Field Trip");

        Drawable drawable = this.getResources().getDrawable(R.drawable.supaaaa);
        HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
        //initControls();

        MapView mapview = (MapView) findViewById(R.id.mapView);

        mapview.setBuiltInZoomControls(true);

        mcontroller = mapview.getController();
        // Plot all geo points
        for (Entry<Integer, FieldTripStop> cur : Statics.fieldTripStops.entrySet())
        {
            // get the FieldTripStop object from the current hash table entry
            Statics.currentFTStop = cur.getValue();
            // concatenate numbers before (all) and after (6) the decimal, since
            // geopoints only accept 6 numbers past the decimal.
            theLat = Statics.currentFTStop.latitude;
            theLong = Statics.currentFTStop.longitude;

            /*
             * String manipulation method
             * 
             * theLat = theLat.replace(".","");
             * theLat = theLat.substring(0, 8);
             * theLong = theLong.replace(".","");
             * theLong = theLong.substring(0, 8);
             * point = new GeoPoint((int)(Integer.valueOf(theLat)), (int)(Integer.valueOf(theLong))); 
             */

            theDLat = Double.parseDouble(theLat);
            theDLong = Double.parseDouble(theLong);



            //olay = new OverlayItem(point, "herp", "derp");
            //olayitems.add(olay);
            List<Overlay> mapOverlays = mapview.getOverlays();





            point = new GeoPoint((int)(theDLat*1E6), (int)(theDLong*1E6));
            OverlayItem overlayitem = new OverlayItem(point, "holda, Mundo!", "I'm in Mexico City~!");

            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);

        }
4

1 回答 1

0

您需要 a)只使用一个HelloItemizedOverlay(而不是每个点一个)和 b)在添加标记之前将此覆盖添加到地图populate在您当前的代码中,覆盖在调用时尚未与地图关联,所以地图不会收到任何通知,也不会自行刷新)。

需要检查的一些事项:

  • 您是否为 MapView 指定了您的 api 密钥?*

*) 如何指定您的 api 密钥(在您的活动的布局资源中):

<com.google.android.maps.MapView android:id="@+id/map"
    ...
    android:apiKey="your-api-key-here"/>
于 2012-05-19T19:58:20.203 回答