Search code examples
androiddynamicviewaugmented-realityoverlapping

Android : How to not overlap dynamic views? + dynamic position of views


I develop an Augmented Reality application which display markers on a camera view. The markers are views dynamically created by getting positions in a database. A marker represents a building for example, described by a name and a position (latitude / longitude).

When the application is launched, I get the current location of the phone and display the markers on the screen according to their locations and the orientation of the phone (SensorManager).

The problem is that when markers are in the same direction, they are overlapped. I would like that the markers are arranged one over one.

I tried using a RelativeLayout but it doesn't change anything.

Code for markers Creation :

@Override
public void onResume() {
    super.onResume();


        for(int j=0;j<markers.length;j++) {
            //System.out.println(prefMarkers[j]);
            if(markers[j] == null) {

                RelativeLayout.LayoutParams rParams = new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);

                    markers = new DataList(); // My list of markers
                    for(int i=0; i<markers.size();i++) {
                        markers[i].setOnClickListener(clickListener);
                            //addContentView(markers[j].get(i), params);
                            relative.addView(markers[j].get(i), rParams);
                    }
                }
            }}}

Code when the location change (the markers move) :

public final static void moveSpot(Context c, View tv, GeoPoint p,
        float azimut, Location me, int screenWidth, float roll,
        int screenHeight, float pitch) {
    int angle = // Calculation for the position of the markers
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT);

            lp.leftMargin = -angle * screenWidth / 90;

    lp.addRule(RelativeLayout.ABOVE);

    tv.setLayoutParams(lp);
}

public SensorEventListener createListener() {
    return new SensorEventListener() {
        public void onAccuracyChanged(Sensor _sensor, int _accuracy) {

        }

        public void onSensorChanged(SensorEvent evt) {
            if (evt.sensor.getType() == Sensor.TYPE_ORIENTATION && markers != null) {

                for(int j=0;j<markers.length;j++)
                    moveSpot(context, markers[i], markers[i]
                                    .getGeoPoint(), azimut, myLoc,
                                    mScreenWidth, roll, mScreenHeight, pitch);

        }
    };
}

I think I don't take the right way so how can I do to prevent the overlapping of the markers ? If you need precisions, ask and I'll try to be as precise as possible.

And sorry for my bad english :/


EDIT : Thanks to Martin Revert's response, I solved a part of my problem. Now I don't have problem of overlapping, but of position.

Here a screenshot of my application now :

http://hpics.li/f7c402b

On this picture, we can see there is no problem of overlapping anyway. That I would like to do is in this remake of the screenshot :

http://hpics.li/25a059d

I hope I'm clear this time :x


Solution

  • You need to assign an id to every View you create on your ViewGroup.

    If every marker is a View use something like:

    yourview[j].setId(j);
    

    Do this before you apply your addRule() onto your "lp" layout.

    EDIT:

    You may also consider to take reference of every view position doing something like this:

    lp.addRule(RelativeLayout.ABOVE, yourview[j-1]);