0
我遵循開發者指南Hello Views示例來開發MapView並繪製這兩個地點之間的路徑。我得到了兩個地點,但我無法在它們之間畫出路徑。無法在android mapview中的兩個地點之間繪製路徑?
爲了解決這個問題我通過修改一點點跟着計算器問題
http://stackoverflow.com/questions/2176397
36 upvoted答案。但仍然無法解決問題。
我的代碼如下
package com.softtrends.mapview;
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
public class MapViewEx extends MapActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView = (MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
//mapView.setStreetView(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
MapViewEx2 itemizedOverlay = new MapViewEx2(drawable);
GeoPoint point1 = new GeoPoint(19240000,-99120000);
OverlayItem overlayitem1 = new OverlayItem(point1, "Hola, Mundo!", "I'm in Mexico City!");
GeoPoint point2 = new GeoPoint((int)(1.352566007*1E6),(int)(103.78921587*1E6));
OverlayItem overlayitem2 = new OverlayItem(point2, null, null);
itemizedOverlay.addOverlay(overlayitem1);
itemizedOverlay.addOverlay(overlayitem2);
mapOverlays.add(itemizedOverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
上面一個是我的活動類。我有另一個覆蓋類如下給出
package com.softtrends.mapview;
import java.util.ArrayList;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
public class MapViewEx2 extends ItemizedOverlay<OverlayItem> {
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
Context mContext;
public MapViewEx2(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public MapViewEx2(Drawable defaultMarker, Context context){
super(defaultMarker);
mContext = context;
}
public void addOverlay(OverlayItem overlay) {
mOverlays.add(overlay);
populate();
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}
@Override
public int size() {
return mOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void draw(Canvas canvas, MapView map, boolean shadow){
super.draw(canvas, map, shadow);
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(2);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
Projection projection = map.getProjection();
projection.toPixels(mOverlays.get(0).getPoint(), p1);
projection.toPixels(mOverlays.get(1).getPoint(), p2);
path.moveTo(p2.x, p2.y);
path.moveTo(p1.x, p1.y);
canvas.drawPath(path, mPaint);
}
}
有人請說我在哪裏犯了錯誤。如果可能,請解釋何時調用第二個類的draw()方法?
我寫了一樣,只是修改了一下。 – 2012-02-07 11:06:48
你是否在繪製方法中檢查了直接的兩個位置。因爲它的工作 – Newts 2012-02-07 11:19:21
我得到的兩個位置,但路徑沒有繪製。 – 2012-02-07 11:24:54