如何在没有群集点击的情况下显示infowindow

时间:2017-03-28 06:26:42

标签: android google-maps infowindow markerclusterer

我没有群集和自定义 InfoWindow 的问题。

当我点击群集时,我不想显示Infowindow。 但是,当我单击clusterItem并单击Cluter时,我的应用程序显示Infowindow。它显示最后一次单击ClusterItem的Infowindow。

代码:

public class MapsLActivity extends FragmentActivity implements OnMapReadyCallback, ClusterManager.OnClusterItemClickListener<House>, GoogleMap.OnMapClickListener, ClusterManager.OnClusterClickListener<House>{

private GoogleMap mMap;
private View infoWindow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps_k);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.getUiSettings().setZoomControlsEnabled(true);

    ClusterManager<House> mClusterManager = new ClusterManager<>(this, mMap);

    mMap.setOnMapClickListener(this);
    mMap.setOnMarkerClickListener(mClusterManager);
    mMap.setOnCameraChangeListener(mClusterManager);

    AssetManager assetManager = getResources().getAssets();
    try {
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) assetManager.open("yb_edu.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(ais));
        StringBuilder sb = new StringBuilder();

        int bufferSize = 1024 * 1024;
        char readBuf[] = new char[bufferSize];
        int resultSize = 0;

        while ((resultSize = br.read(readBuf)) != -1) {
            if (resultSize == bufferSize) {
                sb.append(readBuf);
            } else {
                for (int i = 0; i < resultSize; i++) {
                    sb.append(readBuf[i]);
                }
            }
        }

        String jString = sb.toString();
        JSONObject object = new JSONObject(jString);
        JSONArray list = new JSONArray(object.getString("yb_edu"));

        for (int i = 0; i < list.length(); i++) {
            JSONObject inside = list.getJSONObject(i);

            String title = inside.getString("title");
            String lats = inside.getString("lat");
            String lngs = inside.getString("lng");

            double lat = Double.parseDouble(lats);
            double lng = Double.parseDouble(lngs);

            LatLng position = new LatLng(lat, lng);

            Log.v("@@@", "postion : " + position);

            Log.v("@@i=@@", String.valueOf(i));

            i++;

            mClusterManager.addItem(new House(position, title));
        }
        mClusterManager.cluster(); //클러스터 새로고침
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    mClusterManager.setOnClusterItemClickListener(this);
    mClusterManager.setOnClusterClickListener(this);
}

@Override
public boolean onClusterItemClick(final House house) {
    Toast.makeText(this, "onClusterItemClick", Toast.LENGTH_SHORT).show();

    LatLng latLng = house.getLatLng();

    Log.v("@@", "LatLng : " + latLng);

    infoWindow = getLayoutInflater().inflate(R.layout.markerinfo, null);
    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker marker) {
            ((TextView)infoWindow.findViewById(R.id.tx_infoWindowTitle)).setText(house.getTitle());
            ((TextView)infoWindow.findViewById(R.id.tx_infoWindowContent)).setText(house.getLatLng().toString());
            return infoWindow;
        }

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

    return false;
}

@Override
public boolean onClusterClick(Cluster<House> cluster) {
    mMap.moveCamera(CameraUpdateFactory.zoomIn());
    Toast.makeText(this, "onClusterClick", Toast.LENGTH_SHORT).show();
    return false;
}

@Override
public void onMapClick(LatLng latLng) {
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
}

1 个答案:

答案 0 :(得分:0)

最后我解决了....

public class MapsPActivity extends FragmentActivity实现OnMapReadyCallback,GoogleMap.OnCameraMoveListener {

private GoogleMap mMap;
private float pastZoom = 0;
private ArrayList<House> edulList;

private int clusters = 0;
private int marks = 0;

private View infoWindow;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps_m);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    mMap.getUiSettings().setZoomControlsEnabled(true);

    edulList = new ArrayList<>();

    addEdu();

    mMap.setOnMarkerClickListener(null);
    mMap.setInfoWindowAdapter(null);
    mMap.setOnCameraMoveListener(this);

}

private void addEdu() {
    try {
        AssetManager assetManager = getResources().getAssets();
        AssetManager.AssetInputStream ais = (AssetManager.AssetInputStream) assetManager.open("list.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(ais));
        StringBuilder sb = new StringBuilder();

        int bufferSize = 1024 * 1024;
        char readBuf[] = new char[bufferSize];
        int resultSize = 0;

        while ((resultSize = br.read(readBuf)) != -1) {
            if (resultSize == bufferSize) {
                sb.append(readBuf);
            } else {
                for (int i = 0; i < resultSize; i++) {
                    sb.append(readBuf[i]);
                }
            }
        }

        String jString = sb.toString();
        JSONObject object = new JSONObject(jString);
        JSONArray list = new JSONArray(object.getString("list"));

        for (int i = 0; i < list.length(); i++) {
            JSONObject inside = list.getJSONObject(i);

            String title = inside.getString("title");
            String lats = inside.getString("lat");
            String lngs = inside.getString("lng");

            double lat = Double.parseDouble(lats);
            double lng = Double.parseDouble(lngs);

            LatLng position = new LatLng(lat, lng);

            Log.v("@@@", lats + ", " + lngs + ", " + title);

            Log.v("@@i=@@", String.valueOf(i));

            edulList.add(new House(position, title));

            i++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

private void createMarker() {
    mMap.clear();
    mMap.setOnMarkerClickListener(null);
    mMap.setInfoWindowAdapter(null);

    MarkerOptions markerOptions = new MarkerOptions();

    for (int i = 0; i < edulList.size(); i++) {
        LatLng position = edulList.get(i).getLatLng();
        String title = edulList.get(i).getTitle();

        markerOptions.position(position);
        markerOptions.title(title);
        mMap.addMarker(markerOptions).hideInfoWindow();
    }

    mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
        @Override
        public boolean onMarkerClick(Marker marker) {
            infoWindow = getLayoutInflater().inflate(R.layout.markerinfo, null);
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    ((TextView)infoWindow.findViewById(R.id.tx_infoWindowTitle)).setText(marker.getTitle());
                    ((TextView)infoWindow.findViewById(R.id.tx_infoWindowContent)).setText(marker.getPosition().toString());
                    return infoWindow;
                }

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

            return false;

        }
    });
}

private void createCluster() {
    mMap.clear();
    mMap.setOnMarkerClickListener(null);
    mMap.setInfoWindowAdapter(null);

    ClusterManager mClusterManager = new ClusterManager<>(this, mMap);

    for (int i = 0; i < edulList.size(); i++) {
        LatLng position = edulList.get(i).getLatLng();
        String title = edulList.get(i).getTitle();

        mClusterManager.addItem(edulList.get(i));
    }
    mClusterManager.cluster();

    mMap.setOnMarkerClickListener(mClusterManager);

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener() {
        @Override
        public boolean onClusterItemClick(final ClusterItem clusterItem) {
            infoWindow = getLayoutInflater().inflate(R.layout.markerinfo, null);
            mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                @Override
                public View getInfoWindow(Marker marker) {
                    ((TextView)infoWindow.findViewById(R.id.tx_infoWindowTitle)).setText(marker.getTitle());
                    ((TextView)infoWindow.findViewById(R.id.tx_infoWindowContent)).setText(marker.getPosition().toString());
                    return infoWindow;
                }

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

            return false;
        }
    });

    mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener() {
        @Override
        public boolean onClusterClick(Cluster cluster) {
            mMap.setInfoWindowAdapter(null);
            mMap.moveCamera(CameraUpdateFactory.zoomIn());
            return false;
        }
    });
}

@Override
public void onCameraMove() {
    CameraPosition cameraPosition = mMap.getCameraPosition();
    float zoom = cameraPosition.zoom;

    Log.v("@@", "" + pastZoom + " / " + zoom);

    if (pastZoom == zoom) {

    } else {
        if (zoom < 13) {
            if(clusters == 0){
                createCluster();
                marks = 0;
                clusters = 1;
            }

        } else {
            if(marks == 0){
                createMarker();
                marks = 1;
                clusters = 0;
            }
        }
        pastZoom = zoom;
    }
}

}