标记没有显示stackmob查询后,地图上 [英] Markers not showing on map after stackmob query

查看:127
本文介绍了标记没有显示stackmob查询后,地图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图标记添加到我的地图,坐标和名称都包含在我通过查询到stackmob数据库中获取正在停止对象,程序运行正常显示的地图,但由于某些原因,看起来像它不执行addmarker指令,也不会通过,即使环路在addMarkers方法。另外我没有得到错误或任何种类的消息在控制台或LogCat中,我跑出来的想法来解决这个问题。

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));
    }
}

感谢

编辑:如果我手动添加标记,可以说onCreate方法,它会正确显示在地图上。

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

EDIT2:当我把一个try catch语句围绕map.addmarker错误消息写着不上主线程不知道这

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

推荐答案

好了,所以我找到了解决办法,显然stackmob查询一个单独的线程运行,因此,如果哟想在这种情况下修改UI元素,如地图,你必须调用从成功的方法runonUithread。

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();
}

这篇关于标记没有显示stackmob查询后,地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆