从AsyncTask向谷歌地图添加标记

时间:2013-06-21 17:46:52

标签: google-maps android-asynctask

我有一个非常奇怪的问题,它让我发疯。我从AsyncTask向我的地图添加标记,以避免阻塞主线程。在带有4.2.2的Nexus 4中,它工作得很好,但是在平板星系3.2或星系ace用2.3.7时,它会在map.addMarker()时给我一个空指针异常。如果我在OnProgrssUpdate中创建MarkerOption没有错误,则错误是针对MarkerOption的。我检查对象是否为空,但不是。即使我在addMarker之前放了一个log.d并且对象是正确的但是我得到了错误。这里有代码:

    private class MapItems extends AsyncTask<Void, Item, Void> {
        @Override
        protected Void doInBackground(Void... values) {
            int density = getResources().getDisplayMetrics().densityDpi;
            for (int companyId : companies) {
                try {
                    Company company = DBCompanies.getCompany(db_path,
                            GSSettings.DBCODE, companyId);
                    LatLng pos = new LatLng(company.getLatitude(),
                            company.getLongitude());
                    if (pos.latitude == 0 && pos.longitude == 0)
                        continue;

                    Subcategory subcategory = DBParameters.getSubcategory(
                            db_path, GSSettings.DBCODE, company
                                    .getSubcategoriesId()[0], Locale
                                    .getDefault().getLanguage());


    Bitmap bitmap;
                    if (subcategory.getMarker() != null
                            && subcategory.getMarker().length > 1)
                        bitmap = GSTools.getBitmapFromMDPI(
                                subcategory.getMarker(), density);
                    else
                        bitmap = GSTools.getBitmapFromMDPI(
                                subcategory0.getMarker(), density);

                    MarkerOptions markerOptions = new MarkerOptions()
                            .position(pos).anchor(0, 1)
                            .title(company.getName())
                            .snippet(subcategory.getName())
                    .icon(BitmapDescriptorFactory.fromBitmap(bitmap));
            Item item = new Item(markerOptions, company.getId());

            publishProgress(item);
        } catch (Exception e) {
        }
    }
    return null;
}
    @Override
    protected void onProgressUpdate(Item... values) {
        try {
            MarkerOptions markerOptions = values[0].markerOptions;
            Log.d("marker", markerOptions.getTitle());


    //              markerOptions = new MarkerOptions()
    //                      .position(new LatLng(37.750087, -0.848522))
    //                      .anchor(0, 1)
    //                      .title("TT")
    //                      .icon(BitmapDescriptorFactory
    //                              .fromResource(R.drawable.ic_button_map));
            Marker m = map.addMarker(markerOptions);
            haspMap.put(m, values[0].comapnyId);
            Log.d("marker", markerOptions.getTitle());

        } catch (Exception e) {
            Log.d("marker", e.toString());
        }

        super.onProgressUpdate(values);
    }
    }

如果我使用MarkerOption评论的作品。任何想法????

感谢。

1 个答案:

答案 0 :(得分:1)

尝试使用:

mContext.runOnUiThread(new Runnable()
{
 public void run()
 {
    // add your marker here
    Marker m = map.addMarker(markerOptions);
    haspMap.put(m, company.getId());
    Log.d("marker", markerOptions.getTitle());

 }
});

而不是使用进度更新机制来更新您的UI。 mContext是您的活动/上下文

相关问题