Skip to content

Instantly share code, notes, and snippets.

@josh-burton
Created September 22, 2016 04:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save josh-burton/b51f7849273e86b22bf4303a8b0aca4d to your computer and use it in GitHub Desktop.
Save josh-burton/b51f7849273e86b22bf4303a8b0aca4d to your computer and use it in GitHub Desktop.
MapStyleManager - Google Map Styles per zoom level
public final class MapStyleManager implements GoogleMap.OnCameraMoveListener {
private final Context context;
private final GoogleMap map;
private final GoogleMap.OnCameraMoveListener onCameraMoveListener;
private final TreeMap<Float, Integer> styleMap = new TreeMap<>();
@RawRes
private int currentMapStyleRes = 0;
private MapStyleManager(@NonNull Context context, @NonNull GoogleMap map, @Nullable GoogleMap.OnCameraMoveListener onCameraMoveListener) {
this.context = context;
this.map = map;
this.onCameraMoveListener = onCameraMoveListener;
this.map.setOnCameraMoveListener(this);
}
public static @NonNull MapStyleManager attachToMap(@NonNull Context context, @NonNull GoogleMap map, @Nullable GoogleMap.OnCameraMoveListener onCameraMoveListener) {
return new MapStyleManager(context, map, onCameraMoveListener);
}
public static @NonNull MapStyleManager attachToMap(@NonNull Context context, @NonNull GoogleMap map) {
return new MapStyleManager(context, map, null);
}
@Override public void onCameraMove() {
if (null != onCameraMoveListener) {
onCameraMoveListener.onCameraMove();
}
updateMapStyle();
}
public void addStyle(float minZoomLevel, @RawRes int mapStyle) {
this.styleMap.put(minZoomLevel, mapStyle);
updateMapStyle();
}
private void updateMapStyle() {
CameraPosition cameraPosition = this.map.getCameraPosition();
float currentZoomLevel = cameraPosition.zoom;
for (float key : this.styleMap.descendingKeySet()) {
if (currentZoomLevel >= key) {
Integer styleId = this.styleMap.get(key);
setMapStyle(styleId);
return;
}
}
}
private void setMapStyle(@RawRes int styleRes) {
if (this.currentMapStyleRes != styleRes) {
MapStyleOptions style = MapStyleOptions.loadRawResourceStyle(this.context, styleRes);
this.map.setMapStyle(style);
this.currentMapStyleRes = styleRes;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment