2017-03-28 58 views
0

我有小問題羣集和自定義InfoWindow如何顯示infowindow沒有羣集點擊

我不想在單擊羣集時顯示Infowindow。 但我的應用程序顯示Infowindow,當我點擊clusterItem並單擊Cluter。它顯示最後點擊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("@@[email protected]@", 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)); 
} 
} 
+0

評論這個'mMap.setOnM arkerClickListener(mClusterManager);'然後嘗試 –

+0

@Rajesh Kushvaha謝謝commnet我的問題。我會試試這個。但結果是一樣的。 – Jin

回答

0

最後我解決這個問題....

公共類MapsPActivity擴展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("@@[email protected]@", 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; 
    } 
} 

}