1.new一个mapoverlay并添加到map上。 mapoverlay上要绘制数据点标记和TipView:
protected void adjustCacheInMap(final List<CacheData> caches,
CacheData selectedCache) {2.MobileResultMapOverlay如下:
package com.mc.yellowbook.android.map;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Vibrator;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.mc.yellowbook.android.R;
import com.mc.yellowbook.android.app.AppConstants;
import com.mc.yellowbook.android.app.AppUtils;
import com.mc.yellowbook.android.findcache.MobileFindViewCacheActivity;
import com.mc.yellowbook.android.model.BusinessData;
import com.mc.yellowbook.android.model.CacheData;
public class MobileResultMapOverlay extends Overlay implements
android.view.View.OnClickListener {
// offset for tip view
private static int TIP_POSITION_X_OFFERSET = 12;
private static int TIP_POSITION_Y_OFFERSET = 7;
private Bitmap geoPnt = null;
private CacheData selected = null;
private List<CacheData> coll = null;
private List<GeoPoint> points = null;
private Activity activity;
private Vibrator vibrator;
private MapController mapController;
private View mTipView;
private TextView mTextMapTipCacheName, mTextMapTipBusinessName,
mTextMapTipDistance;
private ImageView mImgMapTipViewCache, mImgMapTipCacheDifficulty,
mImgMapTipCacheSize;
public MobileResultMapOverlay(Activity activity, View mTipView,
CacheData selectedCache, List<CacheData> caches, Vibrator vibrator, MapController mapController) {
super();
this.mapController = mapController;
this.vibrator = vibrator;
this.activity = activity;
this.mTipView = mTipView;
coll = caches;
if (selectedCache == null && caches != null && caches.size() != 0) {
// set the first one selected by default
selected = caches.get(0);
}
points = new ArrayList<GeoPoint>();
final List<CacheData> pointsList = new ArrayList<CacheData>();
for (CacheData data : coll) {
BusinessData p = data.getBusiness();
if (p == null) {
continue;
}
if (p.getLatE6() == 0 && p.getLonE6() == 0) {
continue;
}
GeoPoint pnt = new GeoPoint(p.getLatE6(), p.getLonE6());
points.add(pnt);
pointsList.add(data);
}
if (mTipView != null) {
// get View by id
mTextMapTipBusinessName = (TextView) mTipView
.findViewById(R.id.textResultMapTipBusinessName);
mTextMapTipCacheName = (TextView) mTipView
.findViewById(R.id.textResultMapTipCacheName);
mTextMapTipDistance = (TextView) mTipView
.findViewById(R.id.textResultMapTipDistance);
mImgMapTipViewCache = (ImageView) mTipView
.findViewById(R.id.imgResultMapTipViewCache);
mImgMapTipCacheDifficulty = (ImageView) mTipView
.findViewById(R.id.imgResultMapTipDiff);
mImgMapTipCacheSize = (ImageView) mTipView
.findViewById(R.id.imgResultMapTipSize);
// set click listener
mImgMapTipViewCache.setOnClickListener(this);
}
// get bitmap for point show on map
geoPnt = BitmapFactory.decodeResource(activity.getResources(),
R.drawable.mc_cache_flag);
}
@Override
public boolean onTap(GeoPoint click, MapView mapView) {
Projection projection = mapView.getProjection();
Point tapPoint = projection.toPixels(click, null);
Point myPoint = null;
int idx = 0;
double min_dis = Math.pow(25.0, 2);
CacheData cache = null;
for (CacheData data : coll) {
BusinessData p = data.getBusiness();
if (p == null || (p.getLatE6() == 0 && p.getLonE6() == 0)) {
continue;
}
myPoint = projection.toPixels(points.get(idx), null);
double dis = Math.pow(tapPoint.x - myPoint.x, 2.0)
+ Math.pow(tapPoint.y - myPoint.y, 2.0);
if (dis < min_dis) {
min_dis = dis;
cache = data;
}
idx++;
}
// if (cache != null) {
if (cache != selected) {
selected = cache;
// mTipView.setVisibility(View.VISIBLE);
}
// }
if (selected != null) {
// move selected cache to map center
BusinessData selectedBusiness = selected.getBusiness();
mapController.animateTo(new GeoPoint(selectedBusiness.getLatE6(),
selectedBusiness.getLonE6()));
}
return false;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
Projection projection = mapView.getProjection();
Matrix matrix = new Matrix();
float scale = 0.7f;
matrix.postScale(scale, scale);
int width = geoPnt.getWidth();
int height = geoPnt.getHeight();
Bitmap newGeoPnt = null;
newGeoPnt = Bitmap.createBitmap(geoPnt, 0, 0, width, height, matrix,
true);
width = newGeoPnt.getWidth();
height = newGeoPnt.getHeight();
// draw all points
int idx = 0;
for (CacheData data : coll) {
BusinessData p = data.getBusiness();
if (p == null || (p.getLatE6() == 0 && p.getLonE6() == 0)) {
continue;
}
Point point = new Point();
projection.toPixels(points.get(idx), point);
canvas.drawBitmap(newGeoPnt, point.x - width, point.y - height,
null);
idx++;
}
// show tip information
if (selected != null) {
Point point = new Point();
projection.toPixels(new GeoPoint(selected.getBusiness().getLatE6(),
selected.getBusiness().getLonE6()), point);
// show the information in the tip.
MapView.LayoutParams pos = (MapView.LayoutParams) mTipView
.getLayoutParams();
GeoPoint geop = new GeoPoint(selected.getBusiness().getLatE6(),
selected.getBusiness().getLonE6());
pos.point = geop;
pos.x = -width / 2 + TIP_POSITION_X_OFFERSET;
pos.y = -height - mTipView.getHeight() / 2 + TIP_POSITION_Y_OFFERSET;
mTipView.setLayoutParams(pos);
// set the text
mTextMapTipBusinessName.setText(selected.getBusiness().getName());
mTextMapTipCacheName.setText(selected.getTitle());
mTextMapTipDistance.setText(selected.getBusiness().getOneDecimalDistance());
mImgMapTipCacheDifficulty.setImageLevel(selected.getDifficulty());
mImgMapTipCacheSize.setImageLevel(selected.getSize());
mTipView.setVisibility(View.VISIBLE);
mTipView.bringToFront();
} else {
mTipView.setVisibility(View.GONE);
}
return super.draw(canvas, mapView, shadow, when);
}
@Override
public void onClick(View v) {
AppUtils.vibrate(vibrator);
if(v == mImgMapTipViewCache){
// go to view cache page
Intent i = new Intent(activity, MobileFindViewCacheActivity.class);
i.putExtra(AppConstants.INTENT_EXTRA_CACHE_DETAIL,
selected);
activity.startActivity(i);
}
}
}