Android 如何使用毕加索-安卓在谷歌地图的信息窗口中获取图像

Android 如何使用毕加索-安卓在谷歌地图的信息窗口中获取图像,android,google-maps-markers,google-maps-android-api-2,Android,Google Maps Markers,Google Maps Android Api 2,我创建了一个实现MapsView的应用程序。当我点击这个标记时,它会显示一个信息窗口,在这个窗口中,数据是通过毕加索从数据库中检索到的。它工作正常,但问题是信息窗口中的图像没有显示我需要的图像,它仍然显示占位符图像。但是,当我单击地图并再次单击标记时,它将显示我需要的图像。但是,如果我没有单击地图(仍然单击标记),它仍然会显示占位符标记。如何用时间显示我需要的图像(占位符图像将替换为我需要的图像) 这是我的密码 public void plotMarkers(ArrayList<MyMar

我创建了一个实现MapsView的应用程序。当我点击这个标记时,它会显示一个信息窗口,在这个窗口中,数据是通过毕加索从数据库中检索到的。它工作正常,但问题是信息窗口中的图像没有显示我需要的图像,它仍然显示占位符图像。但是,当我单击地图并再次单击标记时,它将显示我需要的图像。但是,如果我没有单击地图(仍然单击标记),它仍然会显示占位符标记。如何用时间显示我需要的图像(占位符图像将替换为我需要的图像)

这是我的密码

public void plotMarkers(ArrayList<MyMarker> markers) {
    if(markers.size() > 0) {
        for (MyMarker myMarker : markers)
        {
            dest = new LatLng(myMarker.getmLatitude(), myMarker.getmLongitude());
            markerOption = new MarkerOptions().position(dest);
            location_marker = mMap.addMarker(markerOption);
            Target target = new PicassoMarker(location_marker);
            targets.add(target);

            ImageView image = new ImageView(this);
            image.setImageResource(R.mipmap.marker);
            int width = image.getDrawable().getIntrinsicWidth();
            int height = image.getDrawable().getIntrinsicHeight();

            Picasso.with(MapsActivity.this).load(myMarker.getmIcon()).resize(width, height).onlyScaleDown().into(target);
            mMarkersHashMap.put(location_marker, myMarker);

            i = getIntent();
            if(i.getBooleanExtra("maps", true)) {
                location_marker.setTitle(i.getStringExtra("nama"));
                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(dest, 16));
            }
            else {
                mMap.setInfoWindowAdapter(new MarkerInfoWindowAdapter());
            }
        }
    }
}

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter
{
    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        View v  = getLayoutInflater().inflate(R.layout.info_windowlayout, null);

        MyMarker myMarker = mMarkersHashMap.get(marker);

        TextView markerLabel = (TextView) v.findViewById(R.id.marker_label);
        markerLabel.setText(myMarker.getmLabel());

        ImageView foto = (ImageView) v.findViewById(R.id.foto);
        Picasso.with(getApplicationContext()).load(myMarker.getmImage()).placeholder(R.layout.progress).error(R.mipmap.error).into(foto);

        TextView anotherLabel = (TextView) v.findViewById(R.id.another_label);
        anotherLabel.setText("Baca selengkapnya...");

        return v;
    }
}
public void绘图标记(ArrayList标记){
如果(markers.size()>0){
用于(MyMarker MyMarker:markers)
{
dest=new LatLng(myMarker.getmLatitude(),myMarker.getmlongstitude());
markerOption=新的MarkerOptions().位置(dest);
位置\标记=mMap.addMarker(标记选项);
目标=新的毕加索标记(位置标记);
目标。添加(目标);
ImageView图像=新的ImageView(此);
image.setImageResource(R.mipmap.marker);
int width=image.getDrawable().getIntrinsicWidth();
int height=image.getDrawable().getIntrinsicHeight();
Picasso.with(mapsacativity.this).load(myMarker.getmIcon()).resize(width,height).onlyScaleDown().into(target);
MmarkerHashMap.put(位置标记,myMarker);
i=getIntent();
if(i.getBooleanExtra(“映射”,true)){
位置标记.设置标题(即getStringExtra(“nama”));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(目的地,16));
}
否则{
mMap.setInfoWindowAdapter(新的MarkerInfoWindowAdapter());
}
}
}
}
公共类MarkerInfoWindowAdapter实现GoogleMap.InfoWindowAdapter
{
@凌驾
公共视图getInfoWindow(标记器){
返回null;
}
@凌驾
公共视图getInfoContents(标记器){
视图v=GetLayoutFlater()。充气(R.layout.info\u windowlayout,null);
MyMarker MyMarker=mMarkersHashMap.get(marker);
TextView标记标签=(TextView)v.findViewById(R.id.marker\u标签);
markerLabel.setText(myMarker.getmLabel());
ImageView foto=(ImageView)v.findViewById(R.id.foto);
Picasso.with(getApplicationContext()).load(myMarker.getmImage()).placeholder(R.layout.progress).error(R.mipmap.error).into(foto);
TextView另一个标签=(TextView)v.findViewById(R.id.另一个标签);
另一个label.setText(“Baca selengkapnya…”);
返回v;
}
}

信息窗口基本上是一个位图,从您填充的视图中捕获。因此,对这些视图的更改(例如毕加索异步更新
图像视图
)不会更新信息窗口

一个有效的解决方案是在毕加索获得并缓存图像后,在
标记上调用
showInfoWindow()
。例如,使用毕加索填充信息窗口,并使用毕加索
回调调用
showInfoWindow()

/***
版权所有(c)2013-2014 Commonware,LLC
根据Apache许可证2.0版(以下简称“许可证”)获得许可;你不可以
除非符合许可证,否则请使用此文件。你可以得到一份
许可证的有效期在http://www.apache.org/licenses/LICENSE-2.0. 除非需要
根据适用法律或书面同意,根据
许可证按“原样”分发,无任何保证或条件
任何形式的,无论是明示的还是暗示的。请参阅许可证以了解具体的信息
管理许可证下的权限和限制的语言。
从《忙碌的程序员指南》到Android开发_
https://commonsware.com/Android
*/
包com.commonware.android.mapsv2.imagepopups;
导入android.annotation.SuppressLint;
导入android.content.Context;
导入android.net.Uri;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.widget.ImageView;
导入android.widget.TextView;
导入java.util.HashMap;
导入com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
导入com.google.android.gms.maps.model.Marker;
导入com.squareup.picasso.Callback;
导入com.squareup.picasso.picasso;
类PopupAdapter实现InfoWindowAdapter{
私有视图弹出窗口=null;
专用充气机=空;
私有HashMap图像=null;
私有上下文ctxt=null;
private int iconWidth=-1;
私有int-iconHeight=-1;
私有标记lastMarker=null;
PopupAdapter(上下文ctxt、布局、充气机、,
HashMap图像){
这个.ctxt=ctxt;
这个。充气机=充气机;
这个。图像=图像;
iconWidth=
ctxt.getResources().getDimensionPixelSize(R.dimen.icon_width);
艾康海特=
ctxt.getResources().getDimensionPixelSize(R.dimen.icon_height);
}
@凌驾
公共视图getInfoWindow(标记器){
返回(空);
}
@SuppressLint(“充气参数”)
@凌驾
公共视图getInfoContents(标记器){
如果(弹出==null){
弹出=充气机。充气(R.layout.popup,null);
}
if(lastMarker==null
||!lastMarker.getId().equals(marker.getId()){
lastMarker=标记;
TextView tv=(TextView)popup.findViewById(R.id.title);
tv.setText(marker.getTitle());
tv=(TextView)popup.findViewById(R.id.snippet);
tv.setText(marker.getSnippet());
Uri image=images.get(marker.getId());
ImageView图标=(ImageView)popup.findViewById(R.id.icon);
if(image==null){
icon.setVisibility(View.GONE);
}
否则{
毕加索。with(ctxt)。load(image)。resize(iconWidth,iconHeight)
.centerCrop().noFade()
/***
  Copyright (c) 2013-2014 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.mapsv2.imagepopups;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.HashMap;
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
import com.google.android.gms.maps.model.Marker;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;

class PopupAdapter implements InfoWindowAdapter {
  private View popup=null;
  private LayoutInflater inflater=null;
  private HashMap<String, Uri> images=null;
  private Context ctxt=null;
  private int iconWidth=-1;
  private int iconHeight=-1;
  private Marker lastMarker=null;

  PopupAdapter(Context ctxt, LayoutInflater inflater,
               HashMap<String, Uri> images) {
    this.ctxt=ctxt;
    this.inflater=inflater;
    this.images=images;

    iconWidth=
        ctxt.getResources().getDimensionPixelSize(R.dimen.icon_width);
    iconHeight=
        ctxt.getResources().getDimensionPixelSize(R.dimen.icon_height);
  }

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

  @SuppressLint("InflateParams")
  @Override
  public View getInfoContents(Marker marker) {
    if (popup == null) {
      popup=inflater.inflate(R.layout.popup, null);
    }

    if (lastMarker == null
        || !lastMarker.getId().equals(marker.getId())) {
      lastMarker=marker;

      TextView tv=(TextView)popup.findViewById(R.id.title);

      tv.setText(marker.getTitle());
      tv=(TextView)popup.findViewById(R.id.snippet);
      tv.setText(marker.getSnippet());

      Uri image=images.get(marker.getId());
      ImageView icon=(ImageView)popup.findViewById(R.id.icon);

      if (image == null) {
        icon.setVisibility(View.GONE);
      }
      else {
        Picasso.with(ctxt).load(image).resize(iconWidth, iconHeight)
               .centerCrop().noFade()
               .placeholder(R.drawable.placeholder)
               .into(icon, new MarkerCallback(marker));
      }
    }

    return(popup);
  }

  static class MarkerCallback implements Callback {
    Marker marker=null;

    MarkerCallback(Marker marker) {
      this.marker=marker;
    }

    @Override
    public void onError() {
      Log.e(getClass().getSimpleName(), "Error loading thumbnail!");
    }

    @Override
    public void onSuccess() {
      if (marker != null && marker.isInfoWindowShown()) {
        marker.showInfoWindow();
      }
    }
  }
}
   Picasso.get()
            .load(it.avatar)
            .placeholder(R.drawable.ic_person_black_24dp)
            .error(R.drawable.ic_person_black_24dp)
            .into(avatarView, object : Callback {
                override fun onSuccess() {
                    if (marker != null && marker.isInfoWindowShown) {
                        marker.hideInfoWindow()
                        marker.showInfoWindow()
                    }
                }

                override fun onError(e: Exception?) {
                    Timber.e("Error loading avatar to info window!")
                }
            })