2016-01-21 81 views
0

我正在使用universal-image-loader-1.9.5.jar。我正試圖在標記上顯示用戶圖像。但圖像沒有被顯示。圖像不能在谷歌地圖標記中使用UiL加載

我做了什麼。

UIL初始化

imageLoader = ImageLoader.getInstance(); 
     defaultOptions = new DisplayImageOptions.Builder() 
     .showImageOnLoading(R.drawable.profile_image) // resource or drawable 
     .showImageForEmptyUri(R.drawable.profile_image) // resource or drawable 
     .showImageOnFail(R.drawable.profile_image) // resource or drawable 
     .resetViewBeforeLoading(false) // default 
     .delayBeforeLoading(1000) 
     .cacheInMemory(false) // default 
     .cacheOnDisk(false) // default 
     .considerExifParams(false) // default 
     .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default 
     .bitmapConfig(Bitmap.Config.ARGB_8888) // default 
     .displayer(new SimpleBitmapDisplayer()) // default 
     .handler(new Handler()) // default 
     .build(); 


File cacheDir = StorageUtils.getCacheDirectory(this); 
config = new ImageLoaderConfiguration.Builder(this) 
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions 
.diskCacheExtraOptions(480, 800, null) 
.threadPoolSize(3) // default 
.threadPriority(Thread.NORM_PRIORITY - 2) // default 
.tasksProcessingOrder(QueueProcessingType.FIFO) // default 
.denyCacheImageMultipleSizesInMemory() 
.memoryCache(new LruMemoryCache(2 * 1024 * 1024)) 
.memoryCacheSize(2 * 1024 * 1024) 
.memoryCacheSizePercentage(13) // default 
.diskCache(new UnlimitedDiskCache(cacheDir)) // default 
.diskCacheSize(50 * 1024 * 1024) 
.diskCacheFileCount(100) 
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default 
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default 
.writeDebugLogs() 
.build(); 
imageLoader.init(config); 

使用InfoWindowAdapter

googleMap.setInfoWindowAdapter(new MyCustomAdapterForItems()); 

public class MyCustomAdapterForItems implements InfoWindowAdapter { 

    private final View myContentsView; 

    MyCustomAdapterForItems() { 
     myContentsView = mainFragmentActivity.getLayoutInflater().inflate(
       R.layout.map_info_window_dialog, null); 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 
     return null; 
    } 

    @Override 
    public View getInfoWindow(Marker marker) { 
     try { 
      JSONObject jsonMarker = new JSONObject(marker.getSnippet()); 
      String name = jsonMarker.getString("name"); 
      double distance = jsonMarker.getDouble("distance"); 
      String imagePath = jsonMarker.getString("profile_pic_address"); 
      String gender = jsonMarker.getString("gender"); 
      String birthDate = jsonMarker.getString("birthdate"); 
      age = hiApplication.getAge(birthDate); 
      TextView tvName = ((TextView) myContentsView 
        .findViewById(R.id.tvMapChatName)); 
      TextView tvAge = ((TextView) myContentsView 
        .findViewById(R.id.tvMapChatAge)); 
      TextView tvDistance = ((TextView) myContentsView 
        .findViewById(R.id.distance)); 
      TextView tvGender = (TextView) myContentsView.findViewById(R.id.tvChatGender); 
      RoundedImageView image = (RoundedImageView) myContentsView.findViewById(R.id.profile_pic); 
      imageLoader.displayImage(imagePath, image , defaultOptions , simpleLoader); //Image loading here 
      tvName.setText(name); 
      if(age == 0) tvAge.setText(""); 
      else tvAge.setText(age+""); 
      tvGender.setText(gender); 
      tvDistance.setText(new DecimalFormat("##.##").format(distance)+ " km."); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return myContentsView; 
    } 
} 

ImageLoadingListener

private class SimpleLoader implements ImageLoadingListener { 

     @Override 
     public void onLoadingCancelled(String arg0, View arg1) { 
      Log.d("d", "image cancelled"); 
     } 

     @Override 
     public void onLoadingComplete(String arg0, View view, Bitmap bitmap) { 
      Log.d("d", "loading complete"); 
      ImageView imageView = (ImageView) view; 
      imageView.setImageBitmap(bitmap); 
     } 

     @Override 
     public void onLoadingFailed(String arg0, View arg1, FailReason arg2) { 
      Log.d("d", "image loading failed"); 

     } 

     @Override 
     public void onLoadingStarted(String arg0, View arg1) { 
      Log.d("d", "image loading started"); 
     } 

    } 

儘管我的日誌顯示圖像已完全加載。這是我的日誌。

01-21 02:38:13.561: D/d(7832): image loading started 
01-21 02:38:13.567: D/ImageLoader(7832): Delay 1000 ms before loading... [http://192.168.1.9:8080/chatme_userimages/[email protected]_100x100] 
01-21 02:38:14.568: D/ImageLoader(7832): Start display image task [http://192.168.1.9:8080/chatme_userimages/[email protected]_100x100] 
01-21 02:38:14.569: D/ImageLoader(7832): Load image from network [http://192.168.1.9:8080/chatme_userimages/[email protected]_100x100] 
01-21 02:38:14.653: D/ImageLoader(7832): Display image in ImageAware (loaded from NETWORK) [http://192.168.1.9:8080/chatme_userimages/[email protected]_100x100] 
01-21 02:38:14.653: D/d(7832): loading complete 

但只有默認drawb R.drawable.profile_image是顯示所有的時間。 有人可以告訴我,我在這裏錯過了什麼嗎?代碼有什麼問題?

+0

檢查圖片的網址是否正確 –

+0

@VivekMishra。圖片路徑'http://192.168.1.9:8080/chatme_userimages/[email protected]。jpg'是正確的,它顯示在我的瀏覽器中,但我不知道爲什麼Imageloader爲我的圖像路徑添加後綴「_100x100」。這使得它錯誤的路徑。 –

+0

我認爲這可能是由於default_options –

回答

0

我發現我的問題的解決方案,下面從Dynamic contents in Maps V2 InfoWindow

Android GoogleMap 2 update information dynamically in InfoWindow with ImageView via Universal Image loader

上面代碼的問題提出了一些建議是getInfoWindow(Marker marker)被下載之前調用圖像因此它沒有顯示更新的用戶界面。

所以我更新infowindow()後使用marker.showInfoWindow()marker.hideInfoWindow()圖片下載。

我在我的代碼中做了以下更改。

@Override 
     public View getInfoContents(Marker marker) { 

      if (FragmentMap.this.marker != null && FragmentMap.this.marker.isInfoWindowShown()) { 

       FragmentMap.this.marker.hideInfoWindow(); 
       FragmentMap.this.marker.showInfoWindow(); 
       FragmentMap.this.marker = null ; 
      } 
      return null; 
     } 



@Override 
     public View getInfoWindow(Marker marker) { 
      try { 
       Log.d("d", "inside getinfowindow"); 
       FragmentMap.this.marker = marker; 
       JSONObject jsonMarker = new JSONObject(marker.getSnippet()); 
       String name = jsonMarker.getString("name"); 
       double distance = jsonMarker.getDouble("distance"); 
       String imagePath = jsonMarker.getString("profile_pic_address"); 
       String gender = jsonMarker.getString("gender"); 
       String birthDate = jsonMarker.getString("birthdate"); 
       age = hiApplication.getAge(birthDate); 
       TextView tvName = ((TextView) myContentsView 
         .findViewById(R.id.tvMapChatName)); 
       TextView tvAge = ((TextView) myContentsView 
         .findViewById(R.id.tvMapChatAge)); 
       TextView tvDistance = ((TextView) myContentsView 
         .findViewById(R.id.distance)); 
       TextView tvGender = (TextView) myContentsView.findViewById(R.id.tvChatGender); 
       RoundedImageView image = (RoundedImageView) myContentsView.findViewById(R.id.profile_pic); 
       imageLoader.displayImage(imagePath, image , defaultOptions , simpleLoader); 
       tvName.setText(name); 
       if(age == 0) tvAge.setText(""); 
       else tvAge.setText(age+""); 
       tvGender.setText(gender); 
       tvDistance.setText(new DecimalFormat("##.##").format(distance)+ " km."); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return myContentsView; 
     } 

內部類SimpleLoader

@Override 
     public void onLoadingComplete(String arg0, final View view, Bitmap bitmap) { 

      markerCustomAdapter.getInfoContents(marker); 
     } 

UPDATE

上面的代碼工作正常,在Android 4.0.4,但是當我試圖在Android棒棒糖此代碼,圖像不正在加載。所以我跟着Add an Image from url into custom InfoWindow google maps v2http://square.github.io/picasso/畢加索圖書館。現在它對我來說工作得很好。

謝謝大家。

0

*您應該創建一個自定義XML佈局文件,然後在充氣的方法getInfoContents(標記爲arg0)

你的圖像準備,但它是沒有得到上的標記,我認爲加載。

試試這個

@Override 
     public View getInfoContents(Marker arg0) { 

      // Getting view from the layout file info_window_layout 
      View v = getLayoutInflater().inflate(R.layout.info_window_layout, null); 

      // Getting the position from the marker 
      LatLng latLng = arg0.getPosition(); 

      // Getting reference to the TextView to set latitude 
      TextView tvLat = (TextView) v.findViewById(R.id.tv_lat); 

      // Getting reference to the TextView to set longitude 
      TextView tvLng = (TextView) v.findViewById(R.id.tv_lng); 

      // Setting the latitude 
      tvLat.setText("Latitude:" + latLng.latitude); 

      // Setting the longitude 
      tvLng.setText("Longitude:"+ latLng.longitude); 

      // Returning the view containing InfoWindow contents 
      return v; 

     } 
    }); 
+0

我嘗試在'getInfoContents(Marker arg0)'中膨脹我的xml,但現在它甚至不顯示我的標記。 –

+0

嘗試從圖像加載器獲取圖像,然後將其設置爲不嘗試直接顯示圖像。 –

+0

此代碼適用於我的代碼中可能存在錯誤 –