OnMapRedady信息窗口

时间:2017-04-23 07:57:09

标签: android google-maps

构建基于位置的应用

我在点击标记时向地图和信息窗口添加标记

代码:

public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    UiSettings uiSettings = mMap.getUiSettings();
    uiSettings.setMyLocationButtonEnabled(false);
    uiSettings.setMapToolbarEnabled(false);

    mMapCoastLocationService.init(mMap);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mMap.setMyLocationEnabled(true);

    // set the markers - new
    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            View view = LayoutInflater.from(context).inflate(R.layout.map_marker_info_contents, null, false);
            //ImageView icon = (ImageView) view.findViewById(R.id.marker_info_window_icon);
            //icon.setImageResource(R.drawable.info);
            TextView title = (TextView) view.findViewById(R.id.marker_info_window_title);
            title.setText(marker.getTitle());
            return view;
        }
    });

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            Integer coastId = mMapCoastLocationService.getCoastIdByMarker(marker);
            startCoastDetailsActivity(coastId);
        }
    });


    mFocusMyLocationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            focusMyLocation();
        }
    });
}

标记的xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/marker_info_window_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:contentDescription="info image"
        android:src="@drawable/info" />

    <TextView
        android:id="@+id/marker_info_window_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"`enter code here`
        android:layout_marginLeft="@dimen/marker_text_margin"
        android:layout_marginStart="@dimen/marker_text_margin"
        android:text="example"
        android:textColor="#000"/>

</LinearLayout>

问题是我第一次运行应用程序时,当我点击一个标记时,我只得到文本而不是图标而点击不起作用

第二次一切都运转良好。

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

这是因为Google Maps InfoWindow在显示之前将InfoWindow呈现为位图(点击时) 这意味着如果您有一个来自Web的图像源或一个大的本地资源加载到InfoWindow内的ImageView,它可能无法在InfoWindow渲染完成之前完成..

第二次加载图片时,InfoWindow已经呈现,因此会出现..

解决方案(使用Glide如下):

public class MarkerInfoWindowAdapter implements GoogleMap.InfoWindowAdapter, GoogleMap.OnInfoWindowClickListener {
    private static final String TAG = MarkerInfoWindowAdapter.class.getName();;

    private static Context mContext;
    private static View mapInfoWindow;
    private static ImageView infoWindowIcon;
    private static TextView infoWindowTitle;
    private static TextView infoWindowDetails;
    private static TextView infoWindowCount;

    public MarkerInfoWindowAdapter(Context context){
        mContext = context;
        mapInfoWindow = ((SessionsActivity) context).getLayoutInflater().inflate(R.layout.marker_info_layout, null);
        infoWindowIcon = (ImageView) mapInfoWindow.findViewById(R.id.session_icon);
        infoWindowTitle = (TextView) mapInfoWindow.findViewById(R.id.title);
        infoWindowDetails = (TextView) mapInfoWindow.findViewById(R.id.snippet);
        infoWindowCount = (TextView) mapInfoWindow.findViewById(R.id.attenders_count);
    }

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

    @Override
    public View getInfoContents(Marker marker) {
        Log.i(TAG, "Refresh");
        refreshInfoWindow(marker);
        return null;
    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        MapHelper.clickInfoItem(getContext(), marker);
    }

    private View manageMarkerInfo(Marker marker){
        if (marker.getTitle() == null || marker.getTitle().length() == 0 || marker.getTitle().equalsIgnoreCase("")) {
            Log.i(TAG, "NO Info");
            marker.hideInfoWindow();
            return null;
        } else{
            Log.i(TAG, "Regular Info");
            return selectMarker(marker);
        }
    }

    private View selectMarker(final Marker marker) {
        if (MapHelper.newSessionMarker != null || marker.getTitle().equalsIgnoreCase(mContext.getString(R.string.session_create))) {
            infoWindowTitle.setText(marker.getTitle());
            infoWindowDetails.setText(LocationHelper.getCountryAddressFromCoords(
                    marker.getPosition().latitude,
                    marker.getPosition().longitude));

            infoWindowCount.setVisibility(View.INVISIBLE);
            Glide.with(mContext)
                    .load(ParseUser.getCurrentUser().getString("user_image_url"))
                    .placeholder(R.drawable.add_session)
                    .bitmapTransform(new CropCircleTransformation(mContext))
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            Log.i(TAG, "Error loading Image");
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target,
                                                       boolean isFromMemoryCache, boolean isFirstResource) {
                            Log.i(TAG, "Success loading Image");
                            getInfoContents(marker);
                            return false;
                        }
                    })
                    .error(R.drawable.add_session)
                    .into(infoWindowIcon);
            return mapInfoWindow;
        } else{
            SessionObject session = MapHelper.selectedSessionByUser;
            infoWindowTitle.setText(session.getSessionMarker().getTitle());
            infoWindowDetails.setText(LocationHelper.getCountryAddressFromCoords(
                    session.getSessionMarker().getPosition().latitude,
                    session.getSessionMarker().getPosition().longitude));

            infoWindowCount.setVisibility(View.VISIBLE);
            if (session.getAttendersCount() > 0) {
                infoWindowCount.setText("" + session.getAttendersCount());
                infoWindowCount.setVisibility(View.VISIBLE);
            }
            else {
                infoWindowCount.setVisibility(View.INVISIBLE);
            }
            if (session != null) {
                Log.d(TAG, "Select Session:");
                Log.d(TAG, "Session Title: " + session.getSessionObject().getString("title"));
                Log.d(TAG, "Session Image: " + session.getSessionObject().getString("session_image_url"));
                Log.d(TAG, "Session GeoPoints: " + marker.getPosition().latitude + ", "
                        + marker.getPosition().longitude);
                Log.d(TAG, "Session Attenders:" + session.getAttendersCount());
                if(session.getSessionObject().getString("address") != null &&
                        session.getSessionObject().getString("address").length() > 0){
                    infoWindowDetails.setText(session.getSessionObject().getString("address"));
                }

                int defaultIcon = Constants.sessionTypesList.get(session.getSessionObject().getInt("type")).getResources()[0];
                try {
                    Glide.with(mContext)
                            .load(session.getSessionObject().getString("session_image_url"))
                            .placeholder(defaultIcon)
                            .bitmapTransform(new CropCircleTransformation(mContext))
                            .listener(new RequestListener<String, GlideDrawable>() {
                                @Override
                                public boolean onException(Exception e, String model, Target<GlideDrawable> target,
                                                           boolean isFirstResource) {
                                    Log.i(TAG, "Error loading Image");
                                    return false;
                                }

                                @Override
                                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target,
                                                               boolean isFromMemoryCache, boolean isFirstResource) {
                                    Log.i(TAG, "Success loading Image");
                                    getInfoContents(marker);
                                    return false;
                                }
                            })
                            .error(defaultIcon)
                            .into(infoWindowIcon);
                    return mapInfoWindow;
                } catch (Exception e1) {
                    e1.printStackTrace();
                    return mapInfoWindow;
                }
            }else{
                return mapInfoWindow;
            }
        }
    }

    private void refreshInfoWindow(Marker marker){
        if (marker != null && marker.isInfoWindowShown()) {
            Log.i(TAG, "Success loading Image");
            marker.hideInfoWindow();
            marker.showInfoWindow();
        }
    }

    private Context getContext() {
        return mContext;
    }

    public SessionObject getSessionFromMarker(Marker marker) {
        for (SessionObject session : ((SessionsActivity)getContext()).sessionsList) {
            if (marker.getId().equals(session.getSessionMarker().getId())) {
                return session;
            }
        }
        return null;
    }
}