锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
/**
* 寮瑰嚭鐨勬皵娉iew
*/
private View popView;
/**
鐩戝惉鍣?br />
*/
private final ItemizedOverlay.OnFocusChangeListener onFocusChangeListener = new ItemizedOverlay.OnFocusChangeListener() {
@Override
public void onFocusChanged(ItemizedOverlay overlay, OverlayItem newFocus) {
//鍒涘緩姘旀場紿楀彛
if (popView != null) {
popView.setVisibility(View.GONE);
}
if (newFocus != null) {
MapView.LayoutParams geoLP = (MapView.LayoutParams) popView.getLayoutParams();
geoLP.point = newFocus.getPoint();//榪欒鐢ㄤ簬popView鐨勫畾浣?br />
TextView title = (TextView) popView.findViewById(R.id.map_bubbleTitle);
title.setText(newFocus.getTitle());
TextView desc = (TextView) popView.findViewById(R.id.map_bubbleText);
if (newFocus.getSnippet() == null || newFocus.getSnippet().length() == 0) {
desc.setVisibility(View.GONE);
} else {
desc.setVisibility(View.VISIBLE);
desc.setText(newFocus.getSnippet());
}
mapView.updateViewLayout(popView, geoLP);
popView.setVisibility(View.VISIBLE);
}
}
};
import java.util.ArrayList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
/**
* 鍦板浘涓婄殑綰垮瀷鍥懼眰:鍖呮嫭涓涓搗鐐?涓涓粓鐐?浠ュ強涔嬮棿鐨勬洸綰?br />
* @author superwang
*/
public class LineItemizedOverlay extends ItemizedOverlay<OverlayItem> {
private static final int LAYER_FLAGS = Canvas.MATRIX_SAVE_FLAG | Canvas.CLIP_SAVE_FLAG
| Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.FULL_COLOR_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG;
/**
* 鐢ㄤ簬淇濆瓨璧風(fēng)偣/緇堢偣鏁版嵁
*/
private final ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
/**
* 鐢ㄤ簬淇濆瓨鏋勬垚鏇茬嚎鐨勭偣鐨勬暟鎹?br />
*/
private final ArrayList<GeoPoint> linePoints = new ArrayList<GeoPoint>();
/**
* @param defaultMarker
*/
public LineItemizedOverlay() {
super(null);
// TODO Auto-generated constructor stub
}
/* (non-Javadoc)
* @see com.google.android.maps.ItemizedOverlay#createItem(int)
*/
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
/* (non-Javadoc)
* @see com.google.android.maps.ItemizedOverlay#size()
*/
@Override
public int size() {
// TODO Auto-generated method stub
return mOverlays.size();
}
/**
* 璋冧環(huán)璧風(fēng)偣/緇堢偣
* description:
* @param overlay
*/
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
/**
* 娣誨姞鏇茬嚎涓殑鐐?br />
* description:
* @param point
*/
public void addLinePoint(GeoPoint point) {
linePoints.add(point);
}
public ArrayList<GeoPoint> getLinePoints() {
return linePoints;
}
/**
* 鐢昏搗鐐?緇堢偣/杞ㄨ抗
*/
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if (!shadow) {
//System.out.println("!!!!!!!!!!!!!!");
canvas.save(LAYER_FLAGS);
//canvas.save();
Projection projection = mapView.getProjection();
int size = mOverlays.size();
Point point = new Point();
Paint paint = new Paint();
paint.setAntiAlias(true);
OverlayItem overLayItem;
//鐢昏搗鐐?緇堢偣
for (int i = 0; i < size; i++) {
overLayItem = mOverlays.get(i);
Drawable marker = overLayItem.getMarker(0);
//marker.getBounds()
/* 璞$礌鐐瑰彇寰楄漿鎹?*/
projection.toPixels(overLayItem.getPoint(), point);
if (marker != null) {
boundCenterBottom(marker);
}
/* 鍦嗗湀 */
//Paint paintCircle = new Paint();
//paintCircle.setColor(Color.RED);
paint.setColor(Color.RED);
canvas.drawCircle(point.x, point.y, 5, paint);
/* 鏂囧瓧璁劇疆 */
/* 鏍囬 */
String title = overLayItem.getTitle();
/* 綆浠?*/
// String snippet = overLayItem.getSnippet();
//
// StringBuffer txt = new StringBuffer();
// if (title != null && !"".equals(title))
// txt.append(title);
//
// if (snippet != null && !"".equals(snippet)) {
// if (txt.length() > 0) {
// txt.append(":");
// }
// txt.append(snippet);
// }
//Paint paintText = new Paint();
if (title != null && title.length() > 0) {
paint.setColor(Color.BLACK);
paint.setTextSize(15);
canvas.drawText(title, point.x, point.y, paint);
}
}
//鐢葷嚎
boolean prevInBound = false;//鍓嶄竴涓偣鏄惁鍦ㄥ彲瑙嗗尯鍩?br />
Point prev = null;
int mapWidth = mapView.getWidth();
int mapHeight = mapView.getHeight();
//Paint paintLine = new Paint();
paint.setColor(Color.RED);
//paint.setPathEffect(new CornerPathEffect(10));
paint.setStrokeWidth(2);
int count = linePoints.size();
//Path path = new Path();
//path.setFillType(Path.FillType.INVERSE_WINDING);
for (int i = 0; i < count; i++) {
GeoPoint geoPoint = linePoints.get(i);
//projection.toPixels(geoPoint, point); //榪欎竴琛屼技涔庢湁闂
point = projection.toPixels(geoPoint, null);
if (prev != null) {
if (point.x >= 0 && point.x <= mapWidth && point.y >= 0 && point.y <= mapHeight) {
if ((Math.abs(prev.x - point.x) > 2 || Math.abs(prev.y - point.y) > 2)) {
//榪欓噷鍒ゆ柇鐐規(guī)槸鍚﹂噸鍚堬紝閲嶅悎鐨勪笉鐢葷嚎錛屽彲鑳戒細瀵艱嚧鐢葷嚎涓嶅湪璺笂
canvas.drawLine(prev.x, prev.y, point.x, point.y, paint);
//path.lineTo(point.x, point.y);
prev = point;
prevInBound = true;
}
} else {
//鍦ㄥ彲瑙嗗尯涓庝箣澶?br />
if (prevInBound) {//鍓嶄竴涓偣鍦ㄥ彲瑙嗗尯鍩熷唴錛屼篃闇瑕佸垝綰?br />
//path.lineTo(point.x, point.y);
canvas.drawLine(prev.x, prev.y, point.x, point.y, paint);
}
prev = point;
prevInBound = false;
}
} else {
//path.moveTo(point.x, point.y);
prev = point;
}
}
//canvas.drawPath(path, paint);
canvas.restore();
//DebugUtils.showMemory();
}
super.draw(canvas, mapView, shadow);
}
}