Skip to content

Commit

Permalink
fix(map): normalize markers size
Browse files Browse the repository at this point in the history
  • Loading branch information
simon-the-shark committed Aug 3, 2024
1 parent cfbd40c commit d8d9d44
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
6 changes: 4 additions & 2 deletions lib/config/map_view_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ abstract class MapWidgetConfig {
);
static const mapType = MapType.normal;

static const mapMarkerOriginWidth = 28;
static const activeMapMarkerOriginWidth = 40;
static const mapMarkerWidth = 22.0;
static const mapMarkerHeight = 34.0;
static const activeMapMarkerWidth = 38.0;
static const activeMapMarkerHeight = 58.0;
}

abstract class BuildingSearchConfig {
Expand Down
60 changes: 26 additions & 34 deletions lib/features/map_view/utils/map_marker_utils.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import "dart:ui" as ui;

import "package:flutter/foundation.dart";
import "package:flutter/services.dart";
import "package:flutter/widgets.dart";
import "package:google_maps_flutter/google_maps_flutter.dart";

Expand All @@ -15,47 +12,42 @@ class MapMarkerUtils {
static late final BitmapDescriptor activeParkingMapMarker;

static Future<void> loadMapMarkerAssets(BuildContext context) async {
final pixelRatio =
kIsWeb ? 1 : MediaQuery.devicePixelRatioOf(context).toInt();

buildingMapMarker = await _AssetScaledLoader.loadScaledBitmapDescriptor(
final configuration = context.mapMarkerConfiguration;
final activeMarkerConfig = context.activeMapMarkerConfiguration;
buildingMapMarker = await BitmapDescriptor.asset(
configuration,
Assets.mapMarkers.mapMarker.path,
width: MapWidgetConfig.mapMarkerOriginWidth * pixelRatio,
);

activeBuildingMapMarker =
await _AssetScaledLoader.loadScaledBitmapDescriptor(
activeBuildingMapMarker = await BitmapDescriptor.asset(
activeMarkerConfig,
Assets.mapMarkers.activeMapMarker.path,
width: MapWidgetConfig.activeMapMarkerOriginWidth * pixelRatio,
);
parkingMapMarker = await _AssetScaledLoader.loadScaledBitmapDescriptor(
parkingMapMarker = await BitmapDescriptor.asset(
configuration,
Assets.mapMarkers.parkingMapMarker.path,
width: MapWidgetConfig.mapMarkerOriginWidth * pixelRatio,
);
activeParkingMapMarker =
await _AssetScaledLoader.loadScaledBitmapDescriptor(
activeParkingMapMarker = await BitmapDescriptor.asset(
activeMarkerConfig,
Assets.mapMarkers.activeParkingMapMarker.path,
width: MapWidgetConfig.activeMapMarkerOriginWidth * pixelRatio,
);
}
}

class _AssetScaledLoader {
static Future<BitmapDescriptor> loadScaledBitmapDescriptor(
String assetName, {
required int width,
}) async {
try {
final data = await rootBundle.load(assetName);
final codec = await ui.instantiateImageCodec(
data.buffer.asUint8List(),
targetWidth: width,
extension _ImageConfigurationX on BuildContext {
double get pixelRatio => kIsWeb ? 1.0 : MediaQuery.devicePixelRatioOf(this);

ImageConfiguration get mapMarkerConfiguration => ImageConfiguration(
devicePixelRatio: pixelRatio,
size: const Size(
MapWidgetConfig.mapMarkerWidth,
MapWidgetConfig.mapMarkerHeight,
),
);
ImageConfiguration get activeMapMarkerConfiguration => ImageConfiguration(
devicePixelRatio: pixelRatio,
size: const Size(
MapWidgetConfig.activeMapMarkerWidth,
MapWidgetConfig.activeMapMarkerHeight,
),
);
final fi = await codec.getNextFrame();
final decode = await fi.image.toByteData(format: ui.ImageByteFormat.png);
return BitmapDescriptor.bytes(decode!.buffer.asUint8List());
} on Exception {
return BitmapDescriptor.defaultMarker; // fallback
}
}
}

0 comments on commit d8d9d44

Please sign in to comment.