在标记Google Map上添加infoWindow

时间:2013-04-25 05:23:26

标签: android android-mapview

当用户点按标记时,我想在每个标记集的顶部添加一个信息窗口。这是我的代码。标记很好我只需要在它上面出现infowindow。我怎样才能实现这一点,这是我的代码

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

    }

}

1 个答案:

答案 0 :(得分: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);
            }

        }

设置Overlay 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);
                }