android谷歌地图动态标记

时间:2012-09-09 20:39:58

标签: android google-maps

我想在地图叠加层添加标记(项目)。 我的问题是我的标记设置为ImageView布局。 它有一个9补丁png作为背景,用户的Facebook个人资料图片为src。

我的问题是我懒得下载个人资料图片。如果它还没有加载,我在src上放了一个临时图像,并抛出AsyncTask我正在从Facebook下载个人资料图片。 当图片下载后我想把它放在标记中..

我该怎么办?在所有方法中,标记将Drawable作为对象...

可能与LayerDrawable有关吗?

1 个答案:

答案 0 :(得分:0)

叠加层应该如下

public class MapItemOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    private Context mContext;
    private JSONArray marrJson;
    private JSONObject mjsonObj;

    public MapItemOverlay(Drawable defaultMarker, JSONArray arrJson,
            Context context) {
        super(boundCenterBottom(defaultMarker));
        marrJson = arrJson;
        mContext = context;
    }

    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    @Override
    protected OverlayItem createItem(final int i) {

        OverlayItem result = mOverlays.get(i);

        LayerDrawable marker = (LayerDrawable) mContext.getResources()
                .getDrawable(R.drawable.marker);

        GetFBImageTask task = new GetFBImageTask(i, marker, result);
        task.execute(new Void[] {});



        result.setMarker(boundCenter(marker));

        return result;
    }

    @Override
    public int size() {
        return mOverlays.size();
    }

    public MapItemOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }

    @Override
    protected boolean onTap(int index) {
        return true;
    }

    public class GetFBImageTask extends AsyncTask<Void, Void, LayerDrawable> {
        private LayerDrawable marker;
        private OverlayItem result;
        private int index;

        public GetFBImageTask(int i, LayerDrawable Mmarker, OverlayItem result1) {
            marker = Mmarker;
            result = result1;
            index = i;
        }

        @Override
        protected void onPostExecute(LayerDrawable bmp) {
            super.onPostExecute(bmp);
            if (bmp != null) {
                result.setMarker(boundCenter(bmp));
            }

        }

        @Override
        protected LayerDrawable doInBackground(Void... p) {


try {


Bitmap bmp = null;
                        String[] params = new String[2];
                params[0] = "http://your image url";
                params[1] = your image name;
                URL ulrn = new URL(params[0]);
                HttpURLConnection con = (HttpURLConnection) ulrn
                                    .openConnection();
                InputStream is = con.getInputStream();
                bmp = BitmapFactory.decodeStream(is);


if (null != bmp) {


    if (params[1] != null) {
                    OutputStream fOut = null;
                    File file = new File(Preference.ImagePath,                                              params[1] + ".jpg");


        if (!file.exists()) {
                            fOut = new FileOutputStream(file);
                                 bmp.compress(Bitmap.CompressFormat.PNG,90, fOut);
                                             fOut.flush();
                                             fOut.close();
                        }
                    }

                }




if (bmp != null) {

                        Drawable photo = new BitmapDrawable(
                                mContext.getResources(), bmp);
                        marker.setDrawableByLayerId(
                                R.id.frame,
                                mContext.getResources().getDrawable(
                                        R.drawable.pa3_map_people_img1));
                        marker.setDrawableByLayerId(R.id.photo, photo);
                        marker.setLayerInset(1, 4, 4, 4, 11);
                        return marker;
                }



            } catch (Exception e) {

            }

            return null;
        }
    }


}

drawble / marker.xml应该如下所示

<item
    android:id="@+id/frame"
    android:drawable="@drawable/pa3_map_people_img1"/>
<item
    android:id="@+id/photo"
    android:drawable="@drawable/pa3_map_people_img1"/>