首页 > 解决方案 > 将点添加到基于位置的 AR

问题描述

我尝试开发一个基于位置的 AR 应用程序。我使用 databaseHelper 类来提供我的位置。在 AR 类中,当我使用 databaseHeleper 类不起作用并增强了固定位置中的任何点。当我直接使用数字时,它可以工作。我使用 toast 并了解该位置是从我的 DatabaseHelper 类中正确获取的。

Context context;
private DatabaseHelperClass mDbHelper;
private float[] rotatedProjectionMatrix = new float[16];
private Location currentLocation;
private List<ARPoint> arPoints;
private List<POI> mPoiList;
private PoiAdapter adapter;
private ListView lvPoi;
private ARPoint ar;

public AROverlayView(final Context context) {
    super(context);
    this.context = context;
    mDbHelper = new DatabaseHelperClass(context);
    mDbHelper.openDatabases();
    final int loop = DatabaseHelperClass.j;
    Toast.makeText(context,"" + mDbHelper.getX(2) +","+ mDbHelper.getX(3) ,Toast.LENGTH_SHORT).show();
    xLoc[1] = DatabaseHelperClass.getX(1);
    xLoc[2] = DatabaseHelperClass.getX(2);
    yLoc[1] = DatabaseHelperClass.getY(1);
    yLoc[2] = DatabaseHelperClass.getY(2);

    arPoints = new ArrayList<ARPoint>() {{
        add(new ARPoint("Sun Wheel", xLoc[1],yLoc[1], 1490));
        add(new ARPoint("Sun Wheel",35.023656,51.023569, 1490));

        add(new ARPoint("Linh Ung Pagoda", xLoc[2],yLoc[2], 1490));
        Toast.makeText(context,"" + xLoc[2] +","+yLoc[2] ,Toast.LENGTH_SHORT).show();

    }};

public void updateRotatedProjectionMatrix(float[] rotatedProjectionMatrix) {
    this.rotatedProjectionMatrix = rotatedProjectionMatrix;
    this.invalidate();
}

public void updateCurrentLocation(Location currentLocation){
    this.currentLocation = currentLocation;
    this.invalidate();
}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (currentLocation == null) {
        return;
    }

    final int radius = 30;
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.NORMAL));


    int j=4;
    for (int i = 0; i < arPoints.size(); i ++) {
        float[] currentLocationInECEF = LocationHelper.WSG84toECEF(currentLocation);
        float[] pointInECEF = LocationHelper.WSG84toECEF(arPoints.get(i).getLocation());
        float[] pointInENU = LocationHelper.ECEFtoENU(currentLocation, currentLocationInECEF, pointInECEF);

        float[] cameraCoordinateVector = new float[4];
        Matrix.multiplyMV(cameraCoordinateVector, 0, rotatedProjectionMatrix, 0, pointInENU, 0);

        // cameraCoordinateVector[2] is z, that always less than 0 to display on right position
        // if z > 0, the point will display on the opposite
        if (cameraCoordinateVector[2] < 0) {
            float x  = (0.5f + cameraCoordinateVector[0]/cameraCoordinateVector[3]) * canvas.getWidth();
            float y = (0.5f - cameraCoordinateVector[1]/cameraCoordinateVector[3]) * canvas.getHeight();
            int k =0;
            if (j == 4){
                k = 20;
            }else if (j==3){
                k =15;
            }else if (j == 2){
                k = 10;
            }else {
                k = 5;
            }
            //paint.setColor(Color.argb(1,60,60,60));
            paint.setTextSize(30 + 3*k);
            j--;
            canvas.drawCircle(x, y,k/10* radius, paint);
            canvas.drawText(arPoints.get(i).getName(), x - (30 * arPoints.get(i).getName().length() / 2), y - 80, paint);
        }
    }
  }
}

标签: android

解决方案


推荐阅读