Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android-通过数组在点之间绘制线_Java_Android_Arrays_Mobile - Fatal编程技术网

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

Java Android-通过数组在点之间绘制线,java,android,arrays,mobile,Java,Android,Arrays,Mobile,我试图通过使用for循环在数组中的点之间画线,但似乎有一些问题。我创建了一个for循环,它在数组中循环,但是在尝试使用projection.toPixels(arraylist,point)时;它不断地抛出一个错误。我见过其他人使用它,他们似乎没有出错,请帮助: 我写的代码如下: public class MyPositionOverlay extends ItemizedOverlay<OverlayItem> { public List<GeoPoint> geoP

我试图通过使用for循环在数组中的点之间画线,但似乎有一些问题。我创建了一个for循环,它在数组中循环,但是在尝试使用projection.toPixels(arraylist,point)时;它不断地抛出一个错误。我见过其他人使用它,他们似乎没有出错,请帮助:

我写的代码如下:

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
公共类MyPositionOverlay扩展了ItemizeOverlay{
public List geoPointsArrayList=new ArrayList();
public ArrayList overlayItemList=new ArrayList();
语境;
int listSize=overlayItemList.size();
//向量向量表;
公共MyPositionOverlay(可绘制标记,上下文c){
super(boundCenterBottom(marker));
//TODO自动生成的构造函数存根
填充();
上下文=c;
}
私人最终int mRadius=5;
位置;
公共位置getLocation(){
返回位置;
}
公共无效设置位置(位置位置){;
这个位置=位置;
}
公共void附加项(地质点、字符串标题、字符串片段){
OverlayItem newItem=新的OverlayItem(点、标题、片段);
overlayItemList.add(newItem);
填充();
}
@凌驾
公共空白绘制(画布、地图视图、地图视图、布尔阴影){
Projection=mapView.getProjection();
双纬度=位置。getLatitude()*1E6;
双经度=location.getLongitude()*1E6;
地质点;
地理点=新
地理点(纬度.intValue(),经度.intValue());
地理点前置点=null,当前点=null;
路径linePath=新路径();
点屏幕坐标=新点();
点屏幕坐标2=新点();
点lastPoint=新点();
点linePoint=新点();
if(shadow==false){
//获取当前位置
//将位置转换为屏幕像素
点=新点();
投影。toPixels(地质点、点);
RectF oval=新的RectF(点x-mRadius,点y-mRadius,
点x+mRadius,点y+mRadius);
//设置油漆
油漆=新油漆();
绘制.setARGB(250、255、255、255);
paint.setAntiAlias(真);
paint.setFakeBoldText(真);
//文本设置
绘制文本绘制=新绘制();
textPaint.setColor(Color.BLACK);
油漆背面油漆=新油漆();
背漆.setARGB(175,50,50,50);
backPaint.setAntiAlias(真);
RectF backRect=新的RectF(点x+2+mRadius,
点y-3*mRadius,
点x+65,点y+mRadius);
//画记号笔
画布。绘制椭圆形(椭圆形,油漆);
画布.drawRoundRect(backRect,5,5,backPaint);
canvas.drawText(“我在这里”,
点x+2*m半径,点y,
(油漆);
对于(int j=1;j
出现此错误是因为您试图将类型的对象传递给需要类型的对象的方法。这不起作用,因为
OverlayItem
不是从
GeoPoint
扩展而来。它确实有一个返回覆盖的
GeoPoint
的方法,因此您可以使用该方法

在代码中,您只需更改以下两行:

lineProjection.toPixels(overlayItemList.get(k), from);
lineProjection.toPixels(overlayItemList.get(k + 1), to);
这些行应该是

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

我得到的错误是:类型投影中的toPixels(GeoPoint,Point)方法不适用于我使用代码的参数(OverlayItem,Point),但我看不出有什么错我也看到了这段代码:它似乎与我的相同,是我的ArrayList吗???