Android:地图叠加自定义信息显示在Ballon Overlay上

时间:2013-02-14 07:26:27

标签: android google-maps overlay

我需要在地图上显示地图上的自定义信息,作​​为Pin上的用户磁带。我正在起诉气球地图覆盖,我需要展示的信息来自Web服务 例如,虚拟信息如下  - 时间  - 名称   - 电池状态   - 以及其他一些信息 提前致谢

1 个答案:

答案 0 :(得分:0)

这适用于我,请记得更改包名称,如果您有任何错误,请询问

 package com.utility;

    import java.util.ArrayList;

    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.drawable.Drawable;

    import com.google.android.maps.MapView;
    import com.google.android.maps.OverlayItem;

    public class MyItemizedOverlay extends BalloonItemizedOverlay<OverlayItem> {

        private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
        private Context c;
        private Drawable marker;
        private String mFlag;

        public MyItemizedOverlay(Drawable marker, MapView mapView, String mFlag) {
            super(boundCenter(marker), mapView);
            this.marker = marker;
            c = mapView.getContext();
            this.mFlag = mFlag;
        }

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

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

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

            boundCenterBottom(marker);
        }

        public void removeOverlay(OverlayItem overlay) {
            m_overlays.remove(overlay);
            populate();
        }

        public void clear() {
            m_overlays.clear();
            populate();
        }

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

        @Override
        protected boolean onBalloonTap(int i) {

            return true;
        }

    }


MyItemizedOverlay mItemizedOverlay = new MyItemizedOverlay(marker1, mMapView, "AnyName"); //here marker1 is your pin in drawable like - getResources().getDrawable(R.drawable.pin_red);

//here mMapView is your mapview object that you have defined in your xml

List<Overlay> mapOverlays = mMapView.getOverlays();

//now when you get response from server put this code

GeoPoint newPoint = new GeoPoint(
                                (int) (Double.parseDouble(mLat) * 1E6),
                                (int) (Double.parseDouble(mLong) * 1E6));
                        Geocoder geoCoder = new Geocoder(getBaseContext(),
                                Locale.getDefault());
                        String _destinationAdd = ConvertPointToLocation(newPoint);

                        if (!mUserInfo.mLat.equals("null")
                                || mUserInfo.mLat != null
                                && !mUserInfo.mLong.equals("null")
                                || mUserInfo.mLong != null) {

                            OverlayItem overlayItem = new OverlayItem(
                                    newPoint,

                                            "Location : " + _destinationAdd,
                                    null);
                            itemizedOverlay.clear();

                            itemizedOverlay.addOverlay(overlayItem);
                            mapOverlays.add(itemizedOverlay);

                            mMapView.getController()
                                    .animateTo(
                                            getPoint(
                                                    Double.parseDouble(mUserInfo.mLat),
                                                    Double.parseDouble(mUserInfo.mLong)));
                            mMapView.getController()
                                    .setCenter(
                                            getPoint(
                                                    Double.parseDouble(mUserInfo.mLat),
                                                    Double.parseDouble(mUserInfo.mLong)));
                            mMapView.getController().setZoom(15);
                        }
                        mMapView.invalidate();