0

I want to add an info window on top of Each marker set when user tap on a marker. Here is my code. The marker is fine i need only the infowindow appears on top of it. how can i achieve this, here is my code

public class MainActivity extends MapActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        mapView.setSatellite(true);

        Drawable makerDefault = this.getResources().getDrawable(R.drawable.marker_default);
        MyItemizedOverlay itemizedOverlay = new MyItemizedOverlay(makerDefault);

        Drawable windmill = getResources().getDrawable(R.drawable.windmill);
        Drawable bigBen = getResources().getDrawable(R.drawable.big_ben);
        Drawable eiffelTower = getResources().getDrawable(R.drawable.eiffel_tower);

        itemizedOverlay.addOverlayItem(52372991, 4892655, "Amsterdam", windmill);
        itemizedOverlay.addOverlayItem(51501851, -140623, "London", bigBen);
        itemizedOverlay.addOverlayItem(48857522, 2294496, "Paris", eiffelTower);

        mapView.getOverlays().add(itemizedOverlay);

        MapController mc = mapView.getController();
        mc.setCenter(new GeoPoint(51035349, 2370987)); // Dunkerque, Belgium
        mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());

    }

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

    private class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {

        private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

        public MyItemizedOverlay(Drawable defaultMarker) {
            super(boundCenterBottom(defaultMarker));
        }

        public void addOverlayItem(int lat, int lon, String title, Drawable altMarker) {
            GeoPoint point = new GeoPoint(lat, lon);
            OverlayItem overlayItem = new OverlayItem(point, title, null);
            addOverlayItem(overlayItem, altMarker);
        }

        public void addOverlayItem(OverlayItem overlayItem) {
            mOverlays.add(overlayItem);
            populate();
        }

        public void addOverlayItem(OverlayItem overlayItem, Drawable altMarker) {
            overlayItem.setMarker(boundCenterBottom(altMarker));
            addOverlayItem(overlayItem);
        }

        @Override
        protected OverlayItem createItem(int i) {
            return mOverlays.get(i);
        }

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

        @Override
        protected boolean onTap(int index) {
            Toast.makeText(MainActivity.this, getItem(index).getTitle(), Toast.LENGTH_LONG).show();
            return true;
        }

    }

}
4

1 回答 1

1

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

        }

设置叠加 mapview.getOverlays().add(new SitesOverlay());

如果有多个标记,那么

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);
                }
于 2013-04-25T05:28:12.320 回答