Beispiel #1
1
  @Override
  /**
   * - Capture the clicked cluster so we can use it in custom infoWindow - Check overall bounds of
   * items in cluster - If the bounds are empty (all hosts at same place) then let it pop the info
   * window - Otherwise, move the camera to show the bounds of the map
   */
  public boolean onClusterClick(Cluster<HostBriefInfo> cluster) {
    mLastClickedCluster = cluster; // remember for use later in the Adapter

    // Find out the bounds of the hosts currently in cluster
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for (HostBriefInfo host : cluster.getItems()) {
      builder.include(host.getLatLng());
    }
    LatLngBounds bounds = builder.build();

    // If the hosts are not all at the same location, then change bounds of map.
    if (!bounds.southwest.equals(bounds.northeast)) {
      // Offset from edge of map in pixels when exploding cluster
      View mapView = findViewById(R.id.map_fragment);
      int padding_percent = getResources().getInteger(R.integer.cluster_explode_padding_percent);
      int padding = Math.min(mapView.getHeight(), mapView.getWidth()) * padding_percent / 100;
      CameraUpdate cu =
          CameraUpdateFactory.newLatLngBounds(
              bounds, mapView.getWidth(), mapView.getHeight(), padding);
      mMap.animateCamera(cu);
      return true;
    }
    showMultihostSelectDialog((ArrayList<HostBriefInfo>) cluster.getItems());
    return true;
  }
  public void addHeatMap(List<GridPoint> gridPointList) {
    clearMap();
    List<LatLng> list = new ArrayList<LatLng>();
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    LatLng latLng = null;

    for (GridPoint gridPoint : gridPointList) {
      latLng =
          new LatLng(
              gridPoint.getLocation().getLatitude().doubleValue(),
              gridPoint.getLocation().getLongitude().doubleValue());
      list.add(latLng);
      builder.include(latLng);
    }

    if (list.size() < 1) {
      return;
    }

    mProvider = new HeatmapTileProvider.Builder().data(list).build();
    mOverlay = googleMap.addTileOverlay(new TileOverlayOptions().tileProvider(mProvider));

    if (gridPointList.size() > 1) {
      LatLngBounds bounds = builder.build();
      CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
      googleMap.animateCamera(cu);
    } else if (gridPointList.size() > 0) {
      CameraPosition cameraPosition =
          new CameraPosition.Builder().target(latLng).zoom(zoomLevel).bearing(0).tilt(45).build();
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
    markersDisplayed = false;
    heatmapDisplayed = true;
    clusterDisplayed = false;
  }
  private Boolean zoomOnMarkers() {
    try {
      int padding = 150;

      LatLngBounds.Builder builder = new LatLngBounds.Builder();
      for (Marker marker : mapMarkers) {
        builder.include(marker.getPosition());
      }
      LatLngBounds bounds = builder.build();

      CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
      map.animateCamera(cu);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  public void addCluster(List<GridPoint> complaintDtos) {
    clearMap();
    GoogleMapCluster googleMapCluster;
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    ClusterManager<GoogleMapCluster> mClusterManager;
    mClusterManager = new ClusterManager<GoogleMapCluster>(getActivity(), googleMap);
    googleMap.setOnCameraChangeListener(mClusterManager);
    googleMap.setOnMarkerClickListener(mClusterManager);
    for (GridPoint complaintDto : complaintDtos) {
      googleMapCluster =
          new GoogleMapCluster(
              complaintDto.getLocation().getLatitude().doubleValue(),
              complaintDto.getLocation().getLongitude().doubleValue());
      mClusterManager.addItem(googleMapCluster);
      builder.include(googleMapCluster.getPosition());
    }

    if (complaintDtos.size() > 1) {
      LatLngBounds bounds = builder.build();
      CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
      googleMap.animateCamera(cu);
    } else if (complaintDtos.size() > 0) {
      CameraPosition cameraPosition =
          new CameraPosition.Builder()
              .target(
                  new LatLng(
                      complaintDtos.get(0).getLocation().getLatitude().doubleValue(),
                      complaintDtos.get(0).getLocation().getLongitude().doubleValue()))
              .zoom(zoomLevel)
              .bearing(0)
              .tilt(45)
              .build();
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }
    markersDisplayed = false;
    heatmapDisplayed = false;
    clusterDisplayed = true;
  }
  public void addMarkers(List<GridPoint> gridPointList, List<PointEntity> pointEntityList) {
    clearMap();
    Marker m;
    LatLngBounds.Builder builder = new LatLngBounds.Builder();

    for (GridPoint gridPoint : gridPointList) {
      Bitmap icon =
          gridPoint.getScore() > 10
              ? gridPoint.getScore() > 20
                  ? BitmapFactory.decodeResource(getResources(), R.drawable.red_dot)
                  : BitmapFactory.decodeResource(getResources(), R.drawable.yellow_dot)
              : BitmapFactory.decodeResource(getResources(), R.drawable.green_dot);
      float hue =
          gridPoint.getScore() > 10
              ? gridPoint.getScore() > 20
                  ? BitmapDescriptorFactory.HUE_RED
                  : BitmapDescriptorFactory.HUE_YELLOW
              : BitmapDescriptorFactory.HUE_GREEN;

      markerOptions = new MarkerOptions();
      markerOptions.visible(true);
      markerOptions.position(
          new LatLng(
              gridPoint.getLocation().getLatitude().doubleValue(),
              gridPoint.getLocation().getLongitude().doubleValue()));
      markerOptions.draggable(false);
      markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
      // markerOptions.icon(BitmapDescriptorFactory.defaultMarker(hue));

      m = googleMap.addMarker(markerOptions);
      gridMap.put(m.getId(), gridPoint);
      builder.include(m.getPosition());
    }

    for (PointEntity pointEntity : pointEntityList) {
      Bitmap icon =
          pointEntity.getPointType().equals(PointType.PERSON)
              ? BitmapFactory.decodeResource(getResources(), R.drawable.man)
              : pointEntity.getPointType().equals(PointType.POLICE_STATION)
                  ? BitmapFactory.decodeResource(getResources(), R.drawable.police)
                  : BitmapFactory.decodeResource(getResources(), R.drawable.hospital);
      markerOptions = new MarkerOptions();
      markerOptions.visible(true);
      markerOptions.position(
          new LatLng(
              pointEntity.getLocation().getLatitude().doubleValue(),
              pointEntity.getLocation().getLongitude().doubleValue()));
      markerOptions.draggable(false);
      markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));

      m = googleMap.addMarker(markerOptions);
      pointMap.put(m.getId(), pointEntity);
      builder.include(m.getPosition());
    }

    if (gridPointList.size() > 1) {
      LatLngBounds bounds = builder.build();
      CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 0);
      googleMap.animateCamera(cu);
    } else if (gridPointList.size() > 0) {
      CameraPosition cameraPosition =
          new CameraPosition.Builder()
              .target(
                  new LatLng(
                      gridPointList.get(0).getLocation().getLatitude().doubleValue(),
                      gridPointList.get(0).getLocation().getLongitude().doubleValue()))
              .zoom(zoomLevel)
              .bearing(0)
              .tilt(45)
              .build();
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    }

    markersDisplayed = true;
    heatmapDisplayed = false;
    clusterDisplayed = false;
  }