使用多个标记自定义Android Map Balloon

时间:2013-04-25 05:02:55

标签: android android-mapview

在下面的CustomMap.java类中,我想自定义代码,以便我可以添加地理位置数组,多个itemisedOvelay,如CustomMap Part 2中那样。

public class CustomMap extends MapActivity {
    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    Drawable drawable2;
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        mapOverlays = mapView.getOverlays();

        // first overlay
        drawable = getResources().getDrawable(R.drawable.marker);
        itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);

        GeoPoint point = new GeoPoint((int)(51.5174723*1E6),(int)(-0.0899537*1E6));
        CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)", 
                "(M gives Bond his mission in Daimler car)", 
                "http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA@@._V1._SX40_CR0,0,40,54_.jpg");
        itemizedOverlay.addOverlay(overlayItem);

        GeoPoint point2 = new GeoPoint((int)(51.515259*1E6),(int)(-0.086623*1E6));
        CustomOverlayItem overlayItem2 = new CustomOverlayItem(point2, "GoldenEye (1995)", 
                "(Interiors Russian defence ministry council chambers in St Petersburg)", 
                "http://ia.media-imdb.com/images/M/MV5BMzk2OTg4MTk1NF5BMl5BanBnXkFtZTcwNjExNTgzNA@@._V1._SX40_CR0,0,40,54_.jpg");       
        itemizedOverlay.addOverlay(overlayItem2);
        mapOverlays.add(itemizedOverlay);
        // second overlay
        drawable2 = getResources().getDrawable(R.drawable.marker2);
        itemizedOverlay2 = new CustomItemizedOverlay<CustomOverlayItem>(drawable2, mapView);

        GeoPoint point3 = new GeoPoint((int)(51.513329*1E6),(int)(-0.08896*1E6));
        CustomOverlayItem overlayItem3 = new CustomOverlayItem(point3, "Sliding Doors (1998)", 
                "(interiors)", null);
        itemizedOverlay2.addOverlay(overlayItem3);

        GeoPoint point4 = new GeoPoint((int)(51.51738*1E6),(int)(-0.08186*1E6));
        CustomOverlayItem overlayItem4 = new CustomOverlayItem(point4, "Mission: Impossible (1996)", 
                "(Ethan & Jim cafe meeting)", 
                "http://ia.media-imdb.com/images/M/MV5BMjAyNjk5Njk0MV5BMl5BanBnXkFtZTcwOTA4MjIyMQ@@._V1._SX40_CR0,0,40,54_.jpg");       
        itemizedOverlay2.addOverlay(overlayItem4);
        mapOverlays.add(itemizedOverlay2);
        final MapController mc = mapView.getController();
        mc.animateTo(point2);
        mc.setZoom(16);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

代码第2部分:

这不是代码,而是我想要如何进行更改的一点逻辑。总而言之,我尝试使CustomMap.java灵活,以便我可以通过数组向它添加多个点和信息。

public class CustomMap extends MapActivity {

    MapView mapView;
    List<Overlay> mapOverlays;
    Drawable drawable;
    Drawable drawable2;
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay[];
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay2[];
    double[]lat,lon;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        mapOverlays = mapView.getOverlays();

        // first overlay
        drawable = getResources().getDrawable(R.drawable.marker);
        itemizedOverlay = new CustomItemizedOverlay<CustomOverlayItem>(drawable, mapView);

        GeoPoint point[] = new GeoPoint((int)(lat*1E6),(int)(lon*1E6));
        CustomOverlayItem overlayItem = new CustomOverlayItem(point, "Tomorrow Never Dies (1997)", 
                "(M gives Bond his mission in Daimler car)", 
                "http://ia.media-imdb.com/images/M/MV5BMTM1MTk2ODQxNV5BMl5BanBnXkFtZTcwOTY5MDg0NA@@._V1._SX40_CR0,0,40,54_.jpg");
        itemizedOverlay.addOverlay(overlayItem);

        mapOverlays.add(itemizedOverlay);



        final MapController mc = mapView.getController();
        mc.animateTo(point2);
        mc.setZoom(16);

    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

1 个答案:

答案 0 :(得分:0)

设置Overlay mapview.getOverlays()。add(new SitesOverlay());

自定义弹出窗口
SitesOverlay.class

private class SitesOverlay extends ItemizedOverlay<CustomItem> {
            private List<CustomItem> items;
            private View view = null;

            public SitesOverlay() {
                super(null);
                items = new ArrayList<CustomItem>();
                items.add(new CustomItem(pt, busName, "Bendigo", marker));
                boundCenter(marker);
                populate();
            }

            @Override
            protected CustomItem createItem(int i) {
                return (items.get(i));
            }

            @Override
            public void draw(Canvas canvas, MapView mapView, boolean shadow) {
                if (!shadow) {
                    super.draw(canvas, mapView, false);
                }

            }

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

            public void refresh() {
                populate();
            }

            public void clear() {
                items.clear();
                resetLastFocuesIndex();
            }

            public void resetLastFocuesIndex() {
                setLastFocusedIndex(-1);
            }

            @Override
            protected boolean onTap(final int index) {
                if (view != null) {
                    view.setVisibility(View.GONE);
                    getMapView().removeView(view);
                    getMapView().invalidate();
                    view = null;
                }
                view = getLayoutInflater().inflate(R.layout.balloon_overlay, null);
                LinearLayout layout = (LinearLayout) view
                        .findViewById(R.id.balloon_main_layout);
                layout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                        LayoutParams.WRAP_CONTENT));
                view.setBackgroundResource(R.drawable.balloon_overlay_bg_selector);
                ImageView image = (ImageView) view
                        .findViewById(R.id.balloon_disclosure);
                TextView text = (TextView) view
                        .findViewById(R.id.balloon_item_title);
                text.setText(items.get(index).getTitle());

                Projection projection = getMapView().getProjection();
                Point point = new Point();
                projection.toPixels(items.get(index).getPoint(), point);

                int x = (int) (view.getWidth() / 2f);
                int y = -marker.getIntrinsicHeight() - 3;

                MapView.LayoutParams lp = new MapView.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT, items.get(index)
                                .getPoint(), x, y + 50,
                        MapView.LayoutParams.BOTTOM_CENTER);
                getMapView().removeView(view);
                getMapView().invalidate();
                getMapView().addView(view, lp);
                getMapView().invalidate();

                view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!items.isEmpty()) {
                            if (view != null) {
                                view.setVisibility(View.GONE);
                                getMapView().invalidate();
    //                          Intent intent=new Intent(MapLocationActivity.this,);

                            }
                        }
                        getMapView().invalidate();
                    }
                });
                return true;
            }
        }

        class CustomItem extends OverlayItem {
            Drawable marker = null;

            CustomItem(GeoPoint pt, String name, String snippet, Drawable marker) {
                super(pt, name, snippet);
                this.marker = marker;
            }

            @Override
            public Drawable getMarker(int stateBitset) {
                Drawable result = marker;
                setState(result, stateBitset);
                return (result);
            }

        }

如果有多个标记,那么

for (int i = 0; i < arrayList.size(); i++) {
                    slat = Double.parseDouble(arrayList.get(i).getLat());
                    vlong = Double.parseDouble(arrayList.get(i).getvLong());
                    pt = new GeoPoint((int) (slat * 1E6), (int) (vlong * 1E6));
                    Log.e("lat long", "--- "+slat);
                    MapviewActivity.this.mc.animateTo(pt);
                    items.add(new CustomItem(pt, arrayList.get(i).getBuissnessName(), "Bendigo", marker));
                    boundCenter(marker);
                }
在SitesOverlay()构造函数中

执行此操作。