试试这个:
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());
}
}