android - OnMapRedady 信息窗口

标签 android google-maps

构建基于位置的应用

我在点击标记时将标记添加到 map 和信息窗口

代码:

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>

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

第二次一切正常。

有什么帮助吗?

最佳答案

那是因为 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;
    }
}

关于android - OnMapRedady 信息窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43568723/

相关文章:

javascript - 谷歌地图智能信息窗口位置

java - 如何在 Android 应用程序中持久保存一个对象?

android - 在 Android Studio 中导入库项目

java - 如何初始化在 Activity 中定义的界面,该界面是通过 TabLayout 使用 ViewPager 设置的 3 个 fragment 实现的?

google-maps - 我可以使用 Google 街景 API 获取旧照片吗?

javascript - 从谷歌地图边界获取纬度/经度值

android - 带有 AAR 的 gradle 构建失败,没有找到资源错误

android - 如何设置android模拟器的比例因子

javascript - 创建从 C# 到 JavaScript 的动态 JSON

angularjs - 触摸事件不适用于 Angular 中的 Google map