Question

i'm trying to add markers to my map, the coordinates and names are contained in a stop object that i'm getting through a query to a stackmob database, the program runs fine displays the map, but for some reason it looks like its not executing the addmarker instruction, won't even loop through the for in the addMarkers method. Also i'm not getting errors or messages of any kind in the console or LogCat, i'm running out of ideas to solve this.

public class MainActivity extends Activity {

GoogleMap map;
List<Stop> stops=null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    StackMobAndroid.init(getApplicationContext(), 0, "f4e013f5-3e0f-41e2-af2f-3fc2bfa2446b");
   getMarkers();
}

public void getMarkers( )
{
    Stop.query(Stop.class, new StackMobQuery().field(new StackMobQueryField("stop")), new StackMobQueryCallback<Stop>() {

        @Override
        public void success(List<Stop> result) {

            addMarkers(result);
        }

        @Override
        public void failure(StackMobException e) {
            System.out.println("Fail");
        }
    });


}

public void addMarkers(List<Stop> stops)
{

    for(int i=0;i<=stops.size();i++)
    {
        LatLng markerPos = new LatLng(stops.get(i).getLatitude(), stops.get(i).getLongitude());
        System.out.println(markerPos);
        System.out.println(stops.get(i).getName());
        System.out.println(i);
        map.addMarker(new MarkerOptions().title(stops.get(i).getName()).snippet("test").position(markerPos));
    }
}

Thanks

Edit: If i add a a marker manually in, lets say the onCreate method, it will display correctly on the map.

Edit2: When i put a try catch statement around the map.addmarker the error message reads "Not on the main thread" not sure about this.

Was it helpful?

Solution

Okay so i found the solution, apparently the stackmob query runs on a separate thread so if yo want to modify an ui element, like the map in this case , you have to call a runonUithread from the success method.

public void getMarkers( )
{
    Stop.query(Stop.class, new StackMobQuery().field(new StackMobQueryField("stop")), new StackMobQueryCallback<Stop>() {

        @Override
        public void success(List<Stop> result) {

            runThread(result);
        }

        @Override
        public void failure(StackMobException e) {
            System.out.println("Fail");
        }
    });


}


private void runThread(final List<Stop> stops) {

    new Thread() {
        public void run() {
            int i=0;
            while (i++ < 1) {
                try {
                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            for(int j=0;j<stops.size();j++)
                            {
                                LatLng markerPos = new LatLng(stops.get(j).getLatitude(), stops.get(j).getLongitude());
                                System.out.println(markerPos);
                                System.out.println(stops.get(j).getName());
                                System.out.println(j);

                                try {
                                    map.addMarker(new MarkerOptions().title(stops.get(j).getName()).snippet("test").position(markerPos));
                                } catch (Exception e) {
                                    System.err.println(e.getMessage());
                                }

                            }
                        }
                    });
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top