使用asynctask在地图片段上添加标记

时间:2015-05-18 05:13:09

标签: android google-maps android-asynctask

我有一个问题,我需要使用asynctask在地图片段上添加标记。 这里是我的asynctask类

private class LoadViewTask extends AsyncTask<Void, MyTaskParams, Void> {
    private SearchListAdapter adapter;
    // Before running code in separate thread
    @Override
    protected void onPreExecute() {
        String filterRev = "";
        try {
            filterRev = URLEncoder.encode(
                    MainActivity.FILTER_SEARCH_ENTERPRISE, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        final String dummyURL5star = MainActivity.BASE_URL
                + "v_enterprise-select5star.php?filteritem=" + filterRev;

        xResult5star = MainActivity.getRequestSelect(dummyURL5star,
                SearchActivity.this);


        list = (ListView) findViewById(R.id.lvRowsEnterpriseSearch);
        adapter = new SearchListAdapter(SearchActivity.this, listRow);
        list.setAdapter(adapter);
        registerForContextMenu(list);
        //adapter = (SearchListAdapter) list.getAdapter();

    }

    // The code to be executed in a background thread.
    @Override
    protected Void doInBackground(Void... params) {

            /*final String dummyURL4star = MainActivity.BASE_URL
                    + "v_enterprise-select4star.php?filteritem=" + filterRev;
            final String dummyURL3star = MainActivity.BASE_URL
                    + "v_enterprise-select3star.php?filteritem=" + filterRev;
            final String dummyURL2star = MainActivity.BASE_URL
                    + "v_enterprise-select2star.php?filteritem=" + filterRev;
            final String dummyURL1star = MainActivity.BASE_URL
                    + "v_enterprise-select1star.php?filteritem=" + filterRev;   $*/         

            try {
                jObject5 = new JSONObject(xResult5star);
            JSONArray menuitemArray = jObject5.getJSONArray("venterprise");
                for (int i = 0; i < menuitemArray.length(); i++) {
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(KEY_IDENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("identerprise").toString());
                    map.put(KEY_NAMAENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("nama").toString());
                    map.put(KEY_ALAMATENTERPRISE, menuitemArray.getJSONObject(i)
                            .getString("address").toString());
                    map.put(KEY_KATEGORI,
                            menuitemArray.getJSONObject(i).getString("linkkat")
                                    .toString());
                    map.put(KEY_LINK,
                            menuitemArray.getJSONObject(i).getString("linksub")
                                    .toString());
                    map.put(KEY_KAT,
                            menuitemArray.getJSONObject(i).getString("kategori")
                                    .toString());
                    map.put(KEY_SUB,
                            menuitemArray.getJSONObject(i).getString("subkategori")
                                    .toString());
                    map.put(KEY_IDKAT,
                            menuitemArray.getJSONObject(i).getString("idkategori")
                                    .toString());
                    map.put(KEY_IDSUB,
                            menuitemArray.getJSONObject(i)
                                    .getString("idsubkategori").toString());
                    map.put(KEY_THUMB_URL, menuitemArray.getJSONObject(i)
                            .getString("picture").toString());
                    map.put(KEY_HITS,
                            menuitemArray.getJSONObject(i).getString("hit")
                                    .toString());
                    map.put(KEY_NOKONTRAKS, menuitemArray.getJSONObject(i)
                            .getString("nokontrak").toString());
                    map.put(KEY_STAR, "5");

                    LatLng companyPosition = new LatLng(Double.parseDouble(menuitemArray
                            .getJSONObject(i).getString("latitude").toString()),
                            Double.parseDouble(menuitemArray.getJSONObject(i)
                                    .getString("longitude").toString()));
                    String nama = menuitemArray.getJSONObject(i)
                            .getString("nama").toString();
                    String address = menuitemArray.getJSONObject(i)
                            .getString("address").toString();
                    MyTaskParams company = new MyTaskParams(map, companyPosition, nama, address);
                    publishProgress(company);
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

    return null;
    }

    // Update the progress 
    @Override
    protected void onProgressUpdate(MyTaskParams... values) {
        mapCompany = ((MapFragment) getFragmentManager().findFragmentById(
                R.id.mapListSearch)).getMap();
        listRow.add(values[0].companyList);
        mapCompany.addMarker(new MarkerOptions()
        .title(values[0].nama)
        .snippet(values[0].address)
        .position(values[0].position)
        .icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    }

    // after executing the code in the thread
    @Override
    protected void onPostExecute(Void result) {


    }
}

这是我的MyTaskParam类

public class MyTaskParams {

HashMap<String, String> companyList = new HashMap<String, String>();
LatLng position = new LatLng(0, 0);
String nama, address;


MyTaskParams(HashMap<String, String> companyList, LatLng position, String nama, String address) {
    this.companyList = companyList;
    this.position = position;
    this.nama = nama;
    this.address = address;

}

}

我有一些情况,如果我上面的代码,列表将不会出现包括标记。

但是,如果我只传递HashMap并像这样更改MyTaskParam becom:

public class MyTaskParams {

HashMap<String, String> companyList = new HashMap<String, String>();

MyTaskParams(HashMap<String, String> companyList) {
    this.companyList = companyList;

}

}

MyTaskParams company = new MyTaskParams(map);
                    publishProgress(company);

和progressUpdate像这样:

protected void onProgressUpdate(MyTaskParams... values) {

        listRow.add(values[0].companyList);

    }

列表很好看 有没有人有同样的问题? 我怎么能解决这个问题? 感谢

1 个答案:

答案 0 :(得分:0)

您只能在执行前后方法中使用UI。从后台函数获取数据并在后执行方法中填充标记。