自定义InfoWindow,其中包含来自网址

时间:2018-06-18 15:19:21

标签: android image infowindow

您好我已经尝试了这项工作很长一段时间,经过大量的搜索并尝试了许多不同的事情后,我仍然遇到了同样的情况。

我正在尝试创建一个自定义InfoWindow,其中有一个ImageView,我想添加一个关于我感兴趣的地方(POI)的图像。图像确实被加载但是以一种非常奇怪的方式,比如我有5个POI,我所有标记上的图像变成了我列表中最后一个POI的图像。我检查并确认我的POI中没有重复的图像。这意味着我的编码出了问题。

从我的研究中我发现这个问题的一个可能原因是AsyncTask正在与我添加标记的函数并行工作。每次尝试解决这个问题都会导致彻底失败。

这是我的 CustomInfoWindow

的代码
public class CustomInfoWindow implements GoogleMap.InfoWindowAdapter {
    private static final String TAG = "CustomInfoWindow";
    private View window;
    private Context context;
    private POI place;
    private ImageView img;
    private Marker lastMarker = null;
    public CustomInfoWindow(Context context,POI place) {
        this.context = context;
        this.place = place;
        Log.d(TAG, "CustomInfoWindow: PHOTO by POI "+place.getID()+" "+ place.getPhotos());
    }

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

    @Override
    public View getInfoContents(Marker marker) {
        if (window == null){
            window = LayoutInflater.from(context).inflate(R.layout.custom_marker_info,null);
        }

        TextView tv_Title = (TextView)window.findViewById(R.id.marker_title);

        tv_Title.setText(marker.getTitle());


        TextView tv_Snippet = (TextView)window.findViewById(R.id.marker_snippet);
        tv_Snippet.setText( marker.getSnippet());

        img = (ImageView)window.findViewById(R.id.marker_image);

        new DownloadMarkerPhoto(img).execute(place.getPhotos());


        return window;
    }

}

这是 DownloadMarkerPhoto

的代码
public class DownloadMarkerPhoto extends AsyncTask<String, Void, Bitmap> {


 private static String TAG = "DownloadMarkerPhoto";
    private ImageView image;

    public DownloadMarkerPhoto(ImageView image){
            this.image = image;
    }
    @Override
    protected Bitmap doInBackground(String... urls) {
        String url = urls[0];
        Bitmap bmpImg = null;

        try {
            Log.d(TAG, "doInBackground: PHOTOO " + url);
            InputStream in = new java.net.URL(url).openStream();
            bmpImg = BitmapFactory.decodeStream(in);

        } catch (Exception e) {

            Log.e(TAG, e.toString());
        }

        return bmpImg;
    }

    protected void onPostExecute(Bitmap bmpImage){
        try {
            image.setImageBitmap(bmpImage);

        } catch(Exception e) {

            Log.e(TAG, "onPostExecute: ", e );
        }
    }
}

最后这是我用来添加标记的功能

private void addNewMarker(final POI placeInfo){
        mMap.setInfoWindowAdapter(new CustomInfoWindow(MapsActivity.this,placeInfo));
        try {
            String snippet = "ID: " + placeInfo.getID() + "\nCategory: "+ placeInfo.getCategory();
            mMarker = mMap.addMarker(new MarkerOptions()
                    .title(placeInfo.getPOI_name())
                    .position(new LatLng(placeInfo.getLatitude(),placeInfo.getLongitude()))
                    .snippet(snippet));
        }catch (NullPointerException e){
            Log.e(TAG, "moveCamera: NullPointerException thrown: " + e.getMessage() );
        }

    }

0 个答案:

没有答案