无法将解析图像添加到自定义Google地图信息窗口

时间:2016-06-23 20:47:01

标签: android google-maps infowindow

我正在尝试将我在解析中的图像添加到谷歌地图信息窗口,我可以从资源添加图像,但我无法将现有的解析图像加载到我的信息窗口内的图像视图中。我已经在我的应用程序的其他区域加载了解析图像,但似乎信息窗口超出了范围。有没有办法在我的infowindow中加载这个解析图像?我不想使用资源文件,因为图像在解析时是不同的。我的代码的两部分如下:

        class NearbyEventTask extends AsyncTask<String, Void, ArrayList<Item>>
    {
        Random r;
        Context context;
        public NearbyEventTask(Context context){
            r = new Random();
            this.context = context;
        }
        public LatLng getRandomLocation(Location center, double radius) {
            // Convert radius from meters to degrees
            double radiusInDegrees = radius / 111000;

            double u = r.nextDouble();
            double v = r.nextDouble();
            double w = radiusInDegrees * Math.sqrt(u);
            double t = 2 * Math.PI * v;
            double lat = w * Math.cos(t);
            double lon = w * Math.sin(t);

            double new_lat = lat / Math.cos(center.getLongitude());
            return new LatLng(new_lat + center.getLatitude(), lon + center.getLongitude());
        }
        @Override
        protected ArrayList<Item> doInBackground(String... params) {
            ArrayList<Item> list = new ArrayList<Item>();
            ParseQuery<ParseObject> query = ParseQuery.getQuery("Places");
            if(searchType!=null && searchType.length()>0) {
                ArrayList<String> types = new ArrayList<String>();
                for(String type: searchType.split("\\|")) types.add(type);
                query.whereContainedIn("category", types);
            }
            if(lastKnownLocation!=null) {
                query.whereNear("location", new ParseGeoPoint(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude()));
            }
            try {
                List<ParseObject> objects = query.find();
                for(ParseObject obj : objects){
                    ParseGeoPoint point = obj.getParseGeoPoint("location");
                    Item item = new Item(obj.getString("name"), obj.getString("category"), obj.getString("description"), point.getLatitude(), point.getLongitude());
                    item.vicinity = obj.getString("description") + " | "+obj.getDate("event_date");
                    list.add(item);
                    if(obj.getParseFile("icon")!=null) {
                        item.setIcon(obj.getParseFile("icon").getUrl());
                        item.downloadIcon(context);
                    }
                }
            } catch (ParseException e) {
            }
            return list;
        }

        @Override
        protected void onPostExecute(final ArrayList<Item> arrayList) {
            if(isCancelled()) return;
            if(googleMap!=null) {
                googleMap.clear();
                mMarker2Item.clear();
                LatLngBounds.Builder boundBuilder = new LatLngBounds.Builder();
                for (Item item : arrayList) {
                    MarkerOptions opts = new MarkerOptions()
                            .position(item.location())
                            .title(item.name);
                    if(item.iconBitmap!=null){
                        opts = opts.icon(BitmapDescriptorFactory.fromBitmap(item.iconBitmap));
                    }
                    Marker newMarker = googleMap.addMarker(opts);
                    newMarker.setSnippet(item.vicinity);
                    mMarker2Item.put(newMarker, item);
                    boundBuilder.include(item.location());
                }
                try {


                    if (firstTime) {
                        firstTime = false;
                        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundBuilder.build(), 200);
                        googleMap.moveCamera(cameraUpdate);
                        googleMap.animateCamera(cameraUpdate, 1000, null);
                    }
                } catch (Exception ex) {

                }
            } else mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    onPostExecute(arrayList);
                }
            }, 500);
        }
    }

    @Override
    public void onMapReady(final GoogleMap googleMap) {
        googleMap.setMyLocationEnabled(true);
        this.googleMap = googleMap;
        googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker marker) {
                View v = getActivity().getLayoutInflater().inflate(R.layout.maps_infowindow, null);
                v.setLayoutParams(new LinearLayout.LayoutParams((int) (mapFragment.getView().getMeasuredWidth() * .9), LinearLayout.LayoutParams.WRAP_CONTENT));
                ((TextView) v.findViewById(R.id.title)).setText(marker.getTitle());
                ((TextView) v.findViewById(R.id.desc)).setText(marker.getSnippet());
                ImageView icon = (ImageView) v.findViewById(R.id.imageView5);
                icon.getLayoutParams().height = 800; // OR
                icon.getLayoutParams().width = 800;


                ArrayList<Item> list = new ArrayList<Item>();
                ParseQuery<ParseObject> query = ParseQuery.getQuery("Places");
                if(searchType!=null && searchType.length()>0) {
                    ArrayList<String> types = new ArrayList<String>();
                    for(String type: searchType.split("\\|")) types.add(type);
                    query.whereContainedIn("icon", types);
                }
                try {
                    List<ParseObject> objects = query.find();

                    for(ParseObject obj : objects){
                        if(obj.getParseFile("icon")!=null) {

                            Picasso.with(getActivity()).load(obj.getParseFile("icon").getUrl()).into(icon, new MarkerCallback(marker));

                        }
                    }
                } catch (ParseException e) {
                }
                return v;

            }
        }

    );

代码更新:如果您查看顶部的NearbyEventTast类,您可以看到如何实现代码以从解析中获取数据。我试图通过创建一个新的NearbyEventTast来做同样的事情,但我无法加载正确的图像。它总是显示相同的图像(我认为它是解析中的第一个)并显示所有对象而不是相应的图像。任何idiea什么事都在发生?谢谢!

1 个答案:

答案 0 :(得分:1)

因为我不太了解Parse。但我也面临这个问题,将URL中的图像显示在信息窗口中。

首先,infowindow没有显示下载的图像,因为MapFragment将视图渲染为Canvas然后绘制它。您在信息窗口中看到的不是您创建的视图,而是它们的“图片”或“屏幕截图”。你基本上需要在Marker对象上再次调用showInfoWindow(),这将重新渲染Canvas,你的图像现在将可见。

或者您可以使用Picasso库来加载图片。我在我的应用程序中使用Picasso回调选项。

首先,您需要创建一个实现Picasso Callback接口的Class,并在构造函数中接收一个标记,以在图像加载时调用show info窗口。

 public static class MarkerCallback implements Callback {
    private Marker marker;


    public MarkerCallback(Marker marker) {
        this.marker = marker;
    }
    @Override
    public void onSuccess() {
       if (marker != null && marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
            marker.showInfoWindow();
        }
    }

    @Override
    public void onError() {
    }
}

如何使用它。

public View getInfoContents(Marker marker) { 
            View v = getActivity().getLayoutInflater().inflate(R.layout.maps_infowindow, null); 
            v.setLayoutParams(new LinearLayout.LayoutParams((int) (mapFragment.getView().getMeasuredWidth() * .9), LinearLayout.LayoutParams.WRAP_CONTENT)); 
            ((TextView) v.findViewById(R.id.title)).setText(marker.getTitle()); 
            ((TextView) v.findViewById(R.id.desc)).setText(marker.getSnippet()); 
            ImageView markerIcon = (ImageView) v.findViewById(R.id.imageView5); 
            Picasso.with(MainActivity.this).load(imgUrl).into(markerIcon, new MarkerCallback(marker));
        }

希望这会对您有所帮助。