无法在android mapview中的两个地理位置之间绘制路径?

时间:2012-02-07 10:53:15

标签: android google-maps drawing android-canvas

我遵循开发人员指南Hello Views示例来开发MapView并在这两个地理位置之间绘制路径。我得到了两个地理位置,但我无法画出它们之间的路径。

为了解决这个问题,我遵循了stackoverflow问题

http://stackoverflow.com/questions/2176397
通过修改一点来提高36个答案。但我仍然无法解决问题。

我的代码如下

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;
}
}

以上是我的活动课程。我有另一个Overlay类,如下所示

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()方法何时被调用?

1 个答案:

答案 0 :(得分:0)

请按照本教程对您有所帮助。

http://eagle.phys.utk.edu/guidry/android/mapOverlayDemo.html

这也是一种方式。

public void draw(Canvas canvas, MapView mapv, boolean shadow)
         {
             super.draw(canvas, mapv, 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);


         GeoPoint gP1 = new GeoPoint(19240000,-99120000);
             GeoPoint gP2 = new GeoPoint(37423157, -122085008);


             Point p1 = new Point();
             Point p2 = new Point();

             Path path = new Path();

             Projection projection = mapv.getProjection();
             projection.toPixels(gP1, p1);
             projection.toPixels(gP2, p2);

             path.moveTo(p2.x, p2.y);
             path.lineTo(p1.x,p1.y);

             canvas.drawPath(path, mPaint);
         }

     }