0

我制作了一个简单的地图应用程序,其中我放置了一个可点击和可缩放的地图视图,我在地图上放置了一个静态点,具有静态纬度和经度以及一个图钉图像来指出该地点,现在我想要的是当我单击时在该引脚上应该打开一个评论框图像。我的代码如下:

MainActivity.java

package com.example.maps;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.widget.ImageView;

public class MainActivity extends MapActivity {
MapView mapview;


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

        MapController mc =mapview.getController();
        double lat = Double.parseDouble("23.0355018");
        double lon = Double.parseDouble("72.5630625");
        GeoPoint gp = new GeoPoint((int)(lat*1E6),(int) (lon*1E6));
        mc.animateTo(gp);
        mc.setZoom(15);
        mapview.invalidate();
        List<Overlay> mapOverlays = mapview.getOverlays();
        Drawable drawable = this.getResources().getDrawable(R.drawable.pin);
       // Drawable drawable1 = this.getResources().getDrawable(R.drawable.yellowhite1comment);

        AddItemizedOverlay itemizedOverlay =
        new AddItemizedOverlay(drawable, this);

        OverlayItem overlayitem = new OverlayItem(gp, "Hello", "Sample Overlay item");

        itemizedOverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedOverlay);
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return true;
    }
}

AddItemizedOverlay.java

  package com.example.maps;

import java.util.ArrayList;
import java.util.Collection;

import javax.crypto.spec.IvParameterSpec;

import android.R.bool;
import android.R.drawable;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;


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

public class AddItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
MainActivity mainact = new MainActivity();

    public AddItemizedOverlay(Drawable defaultmarker) {

        super(boundCenterBottom(defaultmarker));
        // TODO Auto-generated constructor stub
    }
public AddItemizedOverlay(Drawable defaultmarker,Context context){
    this(defaultmarker);
    this.context=context;

}
    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub

        return mapOverlays.get(i);
    }

    @Override
       protected boolean onTap(int index) {

           OverlayItem item = mapOverlays.get(index);

           //Initialize context in the AddItemizedClass Constructor.
           item.setMarker(context.getResources().getDrawable(R.drawable.yellowhite1comment));

           mapOverlays.add(itemizedOverlay);

           //itemizedOverlay is like a global object;

           itemizedOverlay.populateNow();

           return true;
       }
    public void populateNow()
    {
        populate(); 
    }
    public void addOverlay(OverlayItem overlay) {
        mapOverlays.add(overlay);
         this.populate();
     }
    @Override
    public int size() {
        // TODO Auto-generated method stub
        return 0;
    }
}
4

2 回答 2

0

在 AddItemizedOverlay 类中使​​用它

  @Override
   protected boolean onTap(int index) {

       OverlayItem item = mapOverlays.get(index);

       //Initialize context in the AddItemizedClass Constructor.
       item.setMarker(context.getResources().getDrawable(R.drawable.comment_box));

       mapOverlays.add(itemizedOverlay);

       //itemizedOverlay is like a global object;

       itemizedOverlay.populateNow();

       return true;
   }
于 2013-01-16T04:33:22.727 回答
0

试试这个:

public class MapendPointsOverlay extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> m_overlays = new ArrayList<OverlayItem>();
private BalloonOverlayView m_balloonView;
private OverlayItem m_overlayitem;
private Context m_context;
private int m_viewOffset;
private MapController m_mapController;
private MapView m_mapView;
private int m_screenWidth;
private int m_screenHeight;
private Display m_screen;
private boolean m_fromroutepath = false;
public MapendPointsOverlay(Drawable p_drawable) {
    super(p_drawable);
}
public MapendPointsOverlay(Drawable p_defaultMarker, Context p_context,
        MapView p_mapview, boolean p_fromroutepath) {
    super(boundCenterBottom(p_defaultMarker));
    m_context = p_context;
    m_mapView = p_mapview;
    m_mapController = p_mapview.getController();
    m_viewOffset = 35;
    m_fromroutepath = p_fromroutepath;
    m_screen = ((WindowManager) m_context           .getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
    m_screenWidth = m_screen.getWidth();
    m_screenHeight = m_screen.getHeight();
}
/**
 * Method that creates an OverlayItem from the list of OverlayItem at index
 * given by <b>p_arg0</b>
 */
@Override
protected OverlayItem createItem(int p_arg0) {
    return m_overlays.get(p_arg0);
}
@Override
public int size() {
    return m_overlays.size();
}
@Override
protected boolean onTap(int p_index) {
    boolean m_isRecycled;
    GeoPoint m_point;
    m_point = createItem(p_index).getPoint();
    m_overlayitem = m_overlays.get(p_index);
    if (m_balloonView == null) {
        m_balloonView = new BalloonOverlayView(m_context, m_viewOffset);
        m_isRecycled = false;
    } else {
        m_isRecycled = true;
    }
    m_balloonView.setVisibility(View.GONE);
    m_balloonView.setData(m_overlayitem);
    m_balloonView.setVisibility(View.VISIBLE);
    MapView.LayoutParams m_params = new MapView.LayoutParams(
     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, m_point,MapView.LayoutParams.BOTTOM_CENTER);
    m_params.mode = MapView.LayoutParams.MODE_MAP;
    List<Overlay> m_mapOverlays = m_mapView.getOverlays();
    if (m_mapOverlays.size() > 1) {
        hideOtherBalloons(m_mapOverlays);
    }
    if (m_isRecycled) {
        m_balloonView.setLayoutParams(m_params);
    } else {
        m_mapView.addView(m_balloonView, m_params);
    }
    m_mapController.animateTo(m_point);
    m_mapController.setCenter(m_point);
    return true;
}
/**
 * Sets the visibility of this overlay's balloon view to GONE.
 */
private void hideBalloon() {
    if (m_balloonView != null) {
        m_balloonView.setVisibility(View.GONE);
    }
}
/**
 * Hides the balloon view for any other BalloonItemizedOverlay instances
 * that might be present on the MapView.
 * 
 * @param p_overlays
 *            - list of overlays (including this) on the MapView.
 */
private void hideOtherBalloons(List<Overlay> p_overlays) {
    for (Overlay m_overlay : p_overlays) {
        if (m_overlay instanceof MapendPointsOverlay && m_overlay != this) {
            ((MapendPointsOverlay) m_overlay).hideBalloon();
        }
    }
}
/**
 * Override this method to handle a "tap" on a balloon. By default, does
 * nothing and returns false.
 * 
 * @param p_index
 *            - The index of the item whose balloon is tapped.
 * @return true if you handled the tap, otherwise false.
 */
protected boolean onBalloonTap(int p_index) {
    return false;
}
/**
 * Method to filter all points based on the projection given by
 * <b>p_projection</b>, FrameWidth <b>p_frameWidth</b>, FrameHeight
 * <b>p_frameHeight</b>
 * 
 * @param p_projection
 *            The projection of the mapview
 * @param p_frameWidth
 *            The width of the screen
 * @param p_frameHeight
 *            The height of the screen
 */
public void filterAllPoints(Projection p_projection, int p_frameWidth,
        int p_frameHeight) {
    try {
        for (int m_i = 0; m_i < AppConstants.m_geoItems.length; m_i++) {
            GeoItem m_tempLoc = AppConstants.m_geoItems[m_i];
            GeoPoint m_geopoint = m_tempLoc.getLocation();
            Point m_scPoint = new Point();
            p_projection.toPixels(m_geopoint, m_scPoint);
            if (m_scPoint.x > 20 && m_scPoint.y > 20
                    && m_scPoint.x < (p_frameWidth)
                    && m_scPoint.y < (p_frameHeight)) {
                OverlayItem m_overlayitem = new OverlayItem(m_geopoint, ""
                        + m_tempLoc.m_id, "location"/*
                                                     * Integer.toString(tempLoc
                                                     * .getLocationId())
                                                     */);
                addOverlay(m_overlayitem);
            }
        }
    } catch (Exception p_e) {
         p_e.printStackTrace();
    }
}
/**
 * 
 * Method to filter <b>p_recordCount</b> points based on the projection
 * given by <b>p_projection</b>, FrameWidth <b>p_frameWidth</b>, FrameHeight
 * <b>p_frameHeight</b>
 * 
 * @param p_recordCount
 *            Th number of points to be filtered
 * @param p_projection
 *            The projection of the mapview
 * @param p_frameWidth
 *            The width of the screen
 * @param p_frameHeight
 *            The height of the screen
 */
public void filterPoints(int p_recordCount, Projection p_projection,
        int p_frameWidth, int p_frameHeight) {
    int m_noOfRecords = 0;
    try {
        for (int m_i = 0; m_noOfRecords < p_recordCount
                && m_i <= AppConstants.m_geoItems.length - 1; m_i++) {
            GeoItem m_tempLoc = AppConstants.m_geoItems[m_i];
            GeoPoint m_geopoint = m_tempLoc.getLocation();
            Point m_scPoint = new Point();
            p_projection.toPixels(m_geopoint, m_scPoint);
            if (m_scPoint.x > 20 && m_scPoint.y > 20
                    && m_scPoint.x < (p_frameWidth)
                    && m_scPoint.y < (p_frameHeight)) {
                OverlayItem m_overlayitem = new OverlayItem(m_geopoint, ""
                        + m_tempLoc.m_id, "location"
                addOverlay(m_overlayitem);
                m_noOfRecords++;                }
        }
    } catch (Exception e) {
        // TODO: handle exception
         e.printStackTrace();
    }
}
/**
 * Method to set the first 10 defined points in the Array of geoitems
 * AppConstants.m_first10geoItems
 */
public void setFirst10Points() {
    try {
        for (int m_i = 0; m_i < AppConstants.m_first10geoItems.length; m_i++) {
            GeoItem m_tempLoc = AppConstants.m_first10geoItems[m_i];
            GeoPoint m_geopoint = m_tempLoc.getLocation();
            OverlayItem m_overlayitem = new OverlayItem(m_geopoint, ""
                    + m_tempLoc.m_id, "location"
            addOverlay(m_overlayitem);
        }
    } catch (Exception p_e) {
        // TODO: handle exception
    }
}
/**
 * Method to set the overlays on the Mapview given by <b>p_mapView</b>
 * 
 * @param p_mapView
 *            The mapview on which the overlays are to be drawn
 */
public void setLocationOverlays(MapView p_mapView) {
    clearOverlays();
    int m_zoomLevel = p_mapView.getZoomLevel();
    Projection m_mapViewprojection = p_mapView.getProjection();
    try {
        // if (!(m_zoomLevel > 16) && !(m_zoomLevel < 12))
        if (!(m_zoomLevel < 12)) {
            if (AppConstants.m_geoItems != null
                    && AppConstants.m_geoItems.length > 0) {
                if (m_zoomLevel == 12) {
                    if (AppConstants.m_geoItems.length == 1) {
                        filterAllPoints(m_mapViewprojection,
                                (m_screenWidth - 5), (m_screenHeight - 25));
                    } else {
                        if (AppConstants.m_first10geoItems != null) {
                            setFirst10Points();
                        }
                    }
                }
                else if (m_zoomLevel > 12) {
                    // Display 10 Records
                    filterPoints(10, m_mapViewprojection,
                            (m_screenWidth - 5), (m_screenHeight - 25));
                } else {
                    // Display all the records available in the current                     // screen
                    filterAllPoints(m_mapViewprojection,
                            (m_screenWidth - 5), (m_screenHeight - 25));
                }
                p_mapView.invalidate();
            }
        }  
        }
    } catch (Exception p_e) {
    }
}
/**
 * Method to set the listener for the zoom controls of mapview given by
 * <b>p_controls</b>
 * 
 * @param p_controls
 *            The Zoom controls of the mapview
 */
public void setZoomControlListener(ZoomControls p_controls) {
    p_controls.setOnZoomInClickListener(new OnClickListener() {
        public void onClick(View v) {
            hideBalloon();
            if (m_mapView.getZoomLevel() > 16) {
            } else {
                m_mapView.getController().zoomIn();
                setLocationOverlays(m_mapView);
            }
        }
    });
    p_controls.setOnZoomOutClickListener(new OnClickListener() {
        public void onClick(View v) {
            hideBalloon();
            if (m_mapView.getZoomLevel() > 12) {
                m_mapView.getController().zoomOut();
                setLocationOverlays(m_mapView);
            }
        }
    });
}
@Override
public boolean onTouchEvent(MotionEvent p_event, MapView p_mapView) {
    if (!(p_mapView.getZoomLevel() > 16)
            && !(p_mapView.getZoomLevel() < 12)) {
        if (p_event.getAction() == MotionEvent.ACTION_DOWN) {
            hideBalloon();
        }
        if (p_event.getAction() == MotionEvent.ACTION_UP) {
            if (m_fromroutepath) {
                System.out.println("From RoutePath");
            } else {
                setLocationOverlays(p_mapView);
            }
        }
    } else {
        hideBalloon();
        if (p_mapView.getZoomLevel() > 16) {
            p_mapView.getController().setZoom(16);
        }
    }
    return super.onTouchEvent(p_event, p_mapView);
}
/**
 * Method to add the overlay given by <b>p_overlay</b> on the ampview
 * 
 * @param p_overlay
 *            The overlay to be added in the list of overlays on the map
 */
public synchronized void addOverlay(OverlayItem p_overlay) {
    m_overlays.add(p_overlay);
    // m_mapView.getOverlays().add(p_overlay);
    populate();
}
/**
 * Method to clear all the overlays from the mapview
 */
public synchronized void clearOverlays() {
    m_overlays.clear();
    populate();
}}

BallonOverlay 类

 public class BalloonOverlayView extends FrameLayout
  {
private LinearLayout m_layout;
private TextView m_title;
private TextView m_snippet;
/**
 * Create a new BalloonOverlayView.
 * 
 * @param p_context
 *            - The activity context.
 * @param p_balloonBottomOffset
 *            - The bottom padding (in pixels) to be applied when rendering this view.
 */
public BalloonOverlayView(Context p_context, int p_balloonBottomOffset)
{
    super(p_context);
    setPadding(10, 0, 10, p_balloonBottomOffset);
    m_layout = new LinearLayout(p_context);
    m_layout.setVisibility(VISIBLE);
    LayoutInflater m_inflater = (LayoutInflater) p_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View m_view = m_inflater.inflate(R.layout.balloonoverlay, m_layout);
    m_title = (TextView) m_view.findViewById(R.id.balloon_item_title);
    m_snippet = (TextView) m_view.findViewById(R.id.balloon_item_snippet);
    m_snippet.setVisibility(View.GONE);
    ImageView m_close = (ImageView) m_view.findViewById(R.id.close_img_button);
    m_close.setOnClickListener(new OnClickListener(){
        public void onClick(View v)
        {
            m_layout.setVisibility(GONE);
        }
    });     
    FrameLayout.LayoutParams m_params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    m_params.gravity = Gravity.NO_GRAVITY;
    addView(m_layout, m_params);
}   
/**
 * Sets the view data from a given overlay item.
 * 
 * @param p_item
 *            - The overlay item containing the relevant view data (title and snippet).
 */
public void setData(OverlayItem p_item)
{
    m_layout.setVisibility(VISIBLE);
    if (p_item.getTitle() != null)
    {
        m_title.setVisibility(VISIBLE);
        m_title.setText(p_item.getTitle());
    }
    else
    {
        m_title.setVisibility(GONE);
    }       
    if (p_item.getSnippet() != null)
    {
        m_snippet.setVisibility(VISIBLE);
        m_snippet.setText(p_item.getSnippet());
    }
    else
    {
        m_snippet.setVisibility(GONE);
    }
     }  
/**
 * Method to set the view data from a given geopoint
 * 
 * @param p_point
 *            The geopoint
 */
public void setData(GeoPoint p_point)
{
    m_layout.setVisibility(VISIBLE);
    m_title.setText("Latitude=" + p_point.getLatitudeE6() + "\n Longitude=" + p_point.getLongitudeE6());
}
}
于 2013-01-16T04:54:16.183 回答