简体   繁体   English

Android-通过数组在点之间绘制线

[英]Android - drawing lines between points through an array

I am trying to draw lines between points through an array using a for loop but I seem to be having a few issues. 我正在尝试使用for循环在数组中的点之间绘制线,但似乎有一些问题。 I create a for loop which loops through the array but when trying to use projection.toPixels(arraylist,point); 我创建了一个for循环,该循环遍历整个数组,但是在尝试使用projection.toPixels(arraylist,point);时, it keeps throwing up an error. 它不断抛出错误。 I have seen other people use it and they dont seem to get an error, please help: 我已经看到其他人在使用它,但似乎没有出现错误,请帮助:

the code i have written is below: 我写的代码如下:

public class MyPositionOverlay extends ItemizedOverlay<OverlayItem> {

public List<GeoPoint> geoPointsArrayList = new ArrayList<GeoPoint>();
public ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
Context context;
int listSize = overlayItemList.size();
//Vector <overlayItemList> vectorList;
public MyPositionOverlay(Drawable marker, Context c) {
    super(boundCenterBottom(marker));
    // TODO Auto-generated constructor stub
    populate();
    context = c;
}
private final int mRadius = 5;
Location location;
public Location getLocation() {
    return location;
}
public void setLocation(Location location) {;
this.location = location;
}

public void addItem(GeoPoint point, String title, String snippet){
    OverlayItem newItem = new OverlayItem(point, title, snippet);
    overlayItemList.add(newItem);
    populate();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    Projection projection = mapView.getProjection();
    Double latitude = location.getLatitude()*1E6;
    Double longitude = location.getLongitude()*1E6;
    GeoPoint geoPoint; 
    geoPoint = new 
            GeoPoint(latitude.intValue(),longitude.intValue());
    GeoPoint prePoint = null, currentPoint = null;
    Path linePath = new Path();
    Point screenCoords = new Point();
    Point screenCoords2 = new Point();
    Point lastPoint = new Point();
    Point linePoint = new Point();
    if (shadow == false) {
        // Get the current location    
        // Convert the location to screen pixels     
        Point point = new Point();
        projection.toPixels(geoPoint, point);
        RectF oval = new RectF(point.x - mRadius, point.y - mRadius, 
                point.x + mRadius, point.y + mRadius);
        // Setup the paint
        Paint paint = new Paint();
        paint.setARGB(250, 255, 255, 255);
        paint.setAntiAlias(true);
        paint.setFakeBoldText(true);
        //Text set up
        Paint textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        Paint backPaint = new Paint();
        backPaint.setARGB(175, 50, 50, 50);
        backPaint.setAntiAlias(true);
        RectF backRect = new RectF(point.x + 2 + mRadius, 
                point.y - 3*mRadius,
                point.x + 65, point.y + mRadius);
        // Draw the marker    
        canvas.drawOval(oval, paint);
        canvas.drawRoundRect(backRect, 5, 5, backPaint);
        canvas.drawText("Here I Am", 
                point.x + 2*mRadius, point.y, 
                textPaint);

        for (int j = 1; j < overlayItemList.size();j++){
            linePath.setLastPoint(4, j - 1);
            linePath.lineTo(4, j);
        }
        linePath.close();
        canvas.drawPath(linePath, linePaint);
    } // End of If statement
    super.draw(canvas, mapView, shadow);
} // End of draw method
public void drawLine(MapView mapView){

    System.out.println("Drawing line");
    Path linePath = new Path();
    Canvas lineCanvas = new Canvas();
    Projection lineProjection = mapView.getProjection();


    //Line Settings
    Paint linePaint = new Paint();
    linePaint.setStyle(Style.FILL);
    linePaint.setStrokeWidth(2);
    linePaint.setARGB(0, 0, 0, 255);
    linePaint.setAntiAlias(true);
    linePaint.setStrokeJoin(Paint.Join.ROUND);
    linePaint.setStrokeCap(Paint.Cap.ROUND);
    /****************************************************************************/

    for (int k = 0; k<overlayItemList.size(); k++){
        if(k == overlayItemList.size() -1){
            break;
        }
        Point from = new Point();
        Point to = new Point();

        lineProjection.toPixels(overlayItemList.get(k), from);
        lineProjection.toPixels(overlayItemList.get(k + 1), to);

        linePath.moveTo(from.x, from.y);
        linePath.lineTo(to.x, to.y);
    }

    lineCanvas.drawPath(linePath, linePaint);


}//End of drawLine method

}* } *

The method toPixels(GeoPoint, Point) in the type Projection is not applicable for the arguments (OverlayItem, Point) Projection类型的toPixels(GeoPoint,Point)方法不适用于参数(OverlayItem,Point)

You're getting this error because you're trying to pass an object of type OverlayItem to a method that expects an object of type GeoPoint . 之所以出现此错误,是因为您试图将OverlayItem类型的对象OverlayItem给需要GeoPoint类型的对象的方法。 This doesn't work because OverlayItem does not extend from GeoPoint . 这不起作用,因为OverlayItem不会从GeoPoint扩展。 It does have a getPoint() method that returns the GeoPoint of the overlay though, so you can use that. 它确实具有getPoint()方法,但该方法返回叠加层的GeoPoint ,因此您可以使用它。

In your code you'd just have to change the following two lines: 在代码中,您只需更改以下两行:

lineProjection.toPixels(overlayItemList.get(k), from);
lineProjection.toPixels(overlayItemList.get(k + 1), to);

Those lines should read 这些行应该读

lineProjection.toPixels(overlayItemList.get(k).getPoint(), from);
lineProjection.toPixels(overlayItemList.get(k + 1).getPoint(), to);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM