如何以编程方式显示标记InfoWindow?

时间:2017-03-27 16:53:11

标签: java android google-maps android-studio google-maps-markers


我使用Google地图标记了一些地方。要显示标记,我使用自定义ClusterItem和自定义ClusterRenderer以及自定义InfoWindowAdapter
这是我用来加载标记来映射的代码。

clusterManager = new ClusterManager<MapClusterItem>(getContext(), map);
final CustomClusterRenderer renderer = new CustomClusterRenderer(getContext(), map, clusterManager);

clusterManager.getMarkerCollection().setOnInfoWindowAdapter(new CustomInfoViewAdapter(LayoutInflater.from(getContext())));
map.setInfoWindowAdapter(clusterManager.getMarkerManager());


map.setOnCameraChangeListener(null);
if(placeList != null) {
                for (PlaceItem place : placeList) {
                    MapClusterItem myItem = new MapClusterItem(place.getLatitude(), place.getLongitude(), place.getName(), place.getSnippet(), place.getRating(), place.getPhone(), place.getAddress(), place.getCategories());
                    clusterManager.addItem(myItem);
                    place.setMapRef(myItem);
                }
            }
clusterManager.setRenderer(renderer);

自定义类代码:

MapClusterItem类:

public class MapClusterItem implements ClusterItem {

    private final LatLng mPosition;
    private final String mTitle;
    private final String mSnippet;
    private final String mRating;
    private final String mPhone;
    private final String mAddress;
    private final String mCategories;

    public MapClusterItem(double lat, double lng, String title, String snippet, String rating, String phone, String address, String categories) {
        mPosition = new LatLng(lat, lng);
        this.mTitle = title;
        this.mRating = rating;
        this.mPhone = phone;
        this.mSnippet = snippet;
        this.mAddress = address;
        this.mCategories = categories;
    }

    public LatLng getPosition() {
        return mPosition;
    }

    public String getTitle() {
        return mTitle;
    }

    public String getSnippet() {
        return "{" +
                "'phone': '" + mPhone+"'," +
                "'address': '" + mAddress+"'," +
                "'categories': '" + mCategories+"'," +
                "'rating': '" + mRating+"'" +
                "}";
    }

    public String getRating() {
        return mRating;
    }

    public String getPhone() {
        return mPhone;
    }
}

CustomClusterRenderer类:

public class CustomClusterRenderer extends DefaultClusterRenderer<MapClusterItem> {


    public CustomClusterRenderer(Context context, GoogleMap map, ClusterManager<MapClusterItem> clusterManager) {
        super(context, map, clusterManager);

    }

    @Override
    protected void onBeforeClusterItemRendered(MapClusterItem item, MarkerOptions markerOptions) {
        final BitmapDescriptor markerDescriptor = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE);
        markerOptions.icon(markerDescriptor).snippet(item.getSnippet());
        markerOptions.icon(markerDescriptor).title(item.getTitle());

    }
}

CustomInfoViewAdapter类:

public class CustomInfoViewAdapter implements GoogleMap.InfoWindowAdapter {

    private final LayoutInflater mInflater;


    public CustomInfoViewAdapter(LayoutInflater inflater) {
        this.mInflater = inflater;
    }

    @Override
    public View getInfoWindow(final Marker marker) {
        final View popup = mInflater.inflate(R.layout.map_description, null);

        String phone = "";
        String rating = "";
        String address = "";
        String categories = "";

        ((TextView) popup.findViewById(R.id.business_name)).setText(marker.getTitle());
        //((TextView) popup.findViewById(R.id.business_snippet)).setText(marker.getSnippet());
        try {
            JSONObject json = new JSONObject(marker.getSnippet());
            phone = json.getString("phone");
            rating = json.getString("rating");
            address = json.getString("address");
            categories = json.getString("categories");
            StringBuilder sb = new StringBuilder(address);
            sb.deleteCharAt(0);
            sb.deleteCharAt(sb.length()-1);
            address = sb.toString();
            address = address.replaceAll(",","\n");
            sb = new StringBuilder(categories);
            sb.deleteCharAt(sb.length()-2);
            categories = sb.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ((TextView) popup.findViewById(R.id.business_rating)).setText(rating);
        ((TextView) popup.findViewById(R.id.business_phone)).setText(phone);
        ((TextView) popup.findViewById(R.id.business_address)).setText(address);
        ((TextView) popup.findViewById(R.id.business_categories)).setText(categories);

        return popup;
    }

    @Override
    public View getInfoContents(Marker marker) {
        final View popup = mInflater.inflate(R.layout.map_description, null);

        String phone = "";
        String rating = "";
        String address = "";
        String categories = "";

        ((TextView) popup.findViewById(R.id.business_name)).setText(marker.getTitle());
        try {
            JSONObject json = new JSONObject(marker.getSnippet());
            phone = json.getString("phone");
            rating = json.getString("rating");
            address = json.getString("address");
            categories = json.getString("categories");
            StringBuilder sb = new StringBuilder(address);
            sb.deleteCharAt(0);
            sb.deleteCharAt(sb.length()-1);
            address = sb.toString();
            address = address.replaceAll(",","\n");
            sb = new StringBuilder(categories);
            sb.deleteCharAt(sb.length()-2);
            categories = sb.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ((TextView) popup.findViewById(R.id.business_rating)).setText(rating);
        ((TextView) popup.findViewById(R.id.business_phone)).setText(phone);
        ((TextView) popup.findViewById(R.id.business_address)).setText(address);
        ((TextView) popup.findViewById(R.id.business_categories)).setText(categories);



        return popup;
    }

}

当我按下按钮时,它会以包含所有这些元素的RecyclerView开始活动 在按下项目时,我需要完成当前活动,并在地图上显示所选项目的InfoWindow。
我知道如何返回地图活动并将所选项目传回给它,我不明白如何以编程方式为此项目调用InfoWindow。

有什么想法吗?

0 个答案:

没有答案