使用InfoWindowAdapter在Google Map V2中使用多个标记

时间:2014-05-23 18:07:10

标签: android google-maps android-maps-v2

我在我的android项目中使用谷歌地图V2。我在地图中使用了多个标记,我使用了InfoWindowAdapter,每个标记我都会恢复标记的位置和数据库中的图片。但问题是只出现一张图片(与最后一个标记相关)。

这是我的代码的一部分:

 protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_geolocalisation);


       Intent i = getIntent();
       String LesAgents = (String)i.getSerializableExtra("LesAgentAssocies");

       try {
         json = new JSONArray(LesAgents);

    } catch (JSONException e) {      
        e.printStackTrace();
    }

       map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();



       for (int j = 0; j < json.length(); j++) {
           initImageLoader();
           markers = new Hashtable<String, String>();
           try {
            JSONObject agent = json.getJSONObject(j);
            String pseudo = agent.getString("pseudo");
            double longitude =    
                         Double.parseDouble(agent.getString("longitude"));
            double latitude = 
                         Double.parseDouble(agent.getString("latitude"));
            String image = agent.getString("image");
            String imagePath = image.substring(1, image.length()); 

            markers = new Hashtable<String, String>();
            imageLoader = ImageLoader.getInstance();

            options = new DisplayImageOptions.Builder()
            .showStubImage(R.drawable.ic_launcher)         
            .showImageForEmptyUri(R.drawable.ic_launcher)     
            .cacheInMemory()
            .cacheOnDisc().bitmapConfig(Bitmap.Config.RGB_565).build();

            if (map!=null){
    map.setInfoWindowAdapter(new CustomInfoWindowAdapter());
      Position = new LatLng(longitude, latitude); 
      marker = map.addMarker(new MarkerOptions().position(Position)
              .title(pseudo));  
   markers.put(marker.getId(),   "http://"+getString(R.string.ip)+imagePath);

   map.moveCamera(CameraUpdateFactory.newLatLngZoom(Position, 15));
     map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
 }
 } catch (JSONException e) {         
 e.printStackTrace();
 }
      } 
   }

       private class CustomInfoWindowAdapter implements InfoWindowAdapter {

            private View view;

            public CustomInfoWindowAdapter() {
                view = 
                   getLayoutInflater().inflate(R.layout.custom_info_window,
                        null);
            }

            @Override
            public View getInfoContents(Marker marker) {

                if (GeolocalisationActivity.this.marker != null
                        &&
                   GeolocalisationActivity.this.marker.isInfoWindowShown()) {
                    GeolocalisationActivity.this.marker.hideInfoWindow();
                    GeolocalisationActivity.this.marker.showInfoWindow();
                }
                return null;
            }

            @Override
            public View getInfoWindow(final Marker marker) {
                GeolocalisationActivity.this.marker = marker;

                String url = null;

                if (marker.getId() != null && markers != null && markers.size() > 0) {
                    if ( markers.get(marker.getId()) != null &&
                            markers.get(marker.getId()) != null) {
                        url = markers.get(marker.getId());
                    }
                }
                final ImageView image = ((ImageView) 
                       view.findViewById(R.id.badge));

                if (url != null && !url.equalsIgnoreCase("null")
                        && !url.equalsIgnoreCase("")) {
                    imageLoader.displayImage(url, image, options,
                            new SimpleImageLoadingListener() {
                                @Override
                                public void onLoadingComplete(String imageUri,
                                        View view, Bitmap loadedImage) {
                                    super.onLoadingComplete(imageUri, view,
                                            loadedImage);
                                    getInfoContents(marker);
                                }
                            });
                } else {
                    image.setImageResource(R.drawable.ic_launcher);
                }

                final String title = marker.getTitle();
                final TextView titleUi = ((TextView)
                      view.findViewById(R.id.title));
                if (title != null) {
                    titleUi.setText(title);
                } else {
                    titleUi.setText("");
                }

                final String snippet = marker.getSnippet();
                final TextView snippetUi = ((TextView) view
                        .findViewById(R.id.snippet));
                if (snippet != null) {
                    snippetUi.setText(snippet);
                } else {
                    snippetUi.setText("");
                }

                return view;
            }
        }

         private void initImageLoader() {
            int memoryCacheSize;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ECLAIR) {
                int memClass = ((ActivityManager) 
                        getSystemService(Context.ACTIVITY_SERVICE))
                        .getMemoryClass();
                memoryCacheSize = (memClass / 8) * 1024 * 1024;
            } else {
                memoryCacheSize = 2 * 1024 * 1024;
            }

     final ImageLoaderConfiguration config = new 
                 ImageLoaderConfiguration.Builder(
                    this).threadPoolSize(5)
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    .memoryCacheSize(memoryCacheSize)
                    .memoryCache(new FIFOLimitedMemoryCache(memoryCacheSize-
                                 1000000))
                    .denyCacheImageMultipleSizesInMemory()
                    .discCacheFileNameGenerator(new Md5FileNameGenerator())

                 .tasksProcessingOrder(QueueProcessingType.LIFO).enableLogging() 
                    .build();

            ImageLoader.getInstance().init(config);
        }   

请帮忙!

1 个答案:

答案 0 :(得分:0)

Maps API v2中使用的信息窗口不是&#34; live&#34;观点。您显然正在使用ImageLoader异步设置ImageView内容,这将无效(因为整个信息窗已经被渲染)。

图像到达时再次调用showInfoWindow()就足够了。

来自Info Windows的文档:

  

注意:绘制的信息窗口是而不是实时视图。观点是   在它的时候呈现为图像(使用View.draw(Canvas))   回。这意味着对视图的任何后续更改都不会   通过地图上的信息窗口反映出来。更新信息窗口   稍后(例如,在加载图片后),请致电showInfoWindow()