Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

M3-119 개인전 픽셀 표시하는 화면 만들기 #12

Merged
merged 5 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 49 additions & 5 deletions lib/controllers/map_controller.dart
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';

import '../service/individual_pixel_service.dart';
import '../models/individual_pixel.dart';
import '../service/pixel_service.dart';
import '../widgets/pixel.dart';

class MapController extends GetxController {
final IndividualPixelService individualPixelService = IndividualPixelService();
final PixelService individualPixelService = PixelService();

static const String darkMapStylePath = 'assets/map_style/dark_map_style.txt';
static const String userMarkerId = 'USER';
static const double latPerPixel = 0.000724;
static const double lonPerPixel = 0.000909;
static const int defaultUserId = 1;

final Location location = Location();
late final String darkMapStyle;
late final String mapStyle;
Completer<GoogleMapController> completer = Completer();

late LocationData currentLocation;

RxList<Pixel> pixels = <Pixel>[].obs;
RxList<Marker> markers = <Marker>[].obs;
RxBool isLoading = true.obs;

Expand All @@ -27,9 +34,10 @@ class MapController extends GetxController {
super.onInit();
await _loadMapStyle();
await updateCurrentLocation();
await _updateIndividualPixel();
_createUserMarker();
_trackUserLocation();
await individualPixelService.getIndividualPixels(currentLatitude: currentLocation.latitude!, currentLongitude: currentLocation.longitude!);
_trackPixels();
}

void _trackUserLocation() {
Expand Down Expand Up @@ -62,11 +70,47 @@ class MapController extends GetxController {
}

Future<void> _loadMapStyle() async {
darkMapStyle = await rootBundle.loadString(darkMapStylePath);
mapStyle = await rootBundle.loadString(darkMapStylePath);
}

void _updateMarkerPosition(LocationData newLocation, String markerId) {
markers.removeWhere((marker) => marker.markerId.value == markerId);
_addMarker(LatLng(newLocation.latitude!, newLocation.longitude!), markerId);
}

Future<void> _updateIndividualPixel() async {
List<IndividualPixel> individualPixelList = await individualPixelService.getIndividualPixels(
currentLatitude: currentLocation.latitude!,
currentLongitude: currentLocation.longitude!,
);

pixels = [
for(var pixel in individualPixelList)
Pixel(
x: pixel.x,
y: pixel.y,
pixelId: pixel.pixelId,
polygonId: pixel.pixelId.toString(),
points: _getRectangleFromLatLng(topLeftPoint: LatLng(pixel.latitude, pixel.longitude)),
fillColor: (pixel.userId == defaultUserId) ? Colors.blue.withOpacity(0.3) : Colors.red.withOpacity(0.3),
strokeColor: (pixel.userId == defaultUserId) ? Colors.blue : Colors.red,
strokeWidth: 1,
),
].obs;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pixels.signAll([]) 로도 구현할 수 있습니다~

}

List<LatLng> _getRectangleFromLatLng({required LatLng topLeftPoint}) {
return List<LatLng>.of({
LatLng(topLeftPoint.latitude, topLeftPoint.longitude),
LatLng(topLeftPoint.latitude, topLeftPoint.longitude + lonPerPixel),
LatLng(topLeftPoint.latitude - latPerPixel, topLeftPoint.longitude + lonPerPixel),
LatLng(topLeftPoint.latitude - latPerPixel, topLeftPoint.longitude),
});
}

void _trackPixels() {
Timer.periodic(const Duration(seconds: 30), (timer) {
_updateIndividualPixel();
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

30초 단위로 호출하면 성능이 괜찮겠죠? 걸음수도 동기화 해야되서 뭔가 walkingService 랑도 같이 쓸수도 있을 것 같네요

}
3 changes: 2 additions & 1 deletion lib/screens/map_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ class MapScreen extends StatelessWidget {
onMapCreated: (GoogleMapController ctrl) {
mapController.completer.complete(ctrl);
},
style: mapController.darkMapStyle,
style: mapController.mapStyle,
markers: Set<Marker>.of(mapController.markers),
polygons: Set<Polygon>.of(mapController.pixels),
);
}
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import 'package:dio/dio.dart';
import '../models/individual_pixel.dart';
import '../utils/dio_service.dart';

class IndividualPixelService {
static final IndividualPixelService _instance =
IndividualPixelService._internal();
class PixelService {
static final PixelService _instance =
PixelService._internal();

final Dio dio = DioService().getDio();

IndividualPixelService._internal();
PixelService._internal();

factory IndividualPixelService() {
factory PixelService() {
return _instance;
}

Expand Down
26 changes: 26 additions & 0 deletions lib/widgets/pixel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

class Pixel extends Polygon {
final int x;
final int y;
final int pixelId;

Pixel({
required this.x,
required this.y,
required this.pixelId,
required String polygonId,
super.points,
super.geodesic,
super.visible,
super.fillColor = const Color(0x00000000),
super.strokeColor = const Color(0x00000000),
super.strokeWidth = 0,
List<PatternItem> patterns = const <PatternItem>[],
super.consumeTapEvents,
super.onTap,
}) : super(
polygonId: PolygonId(polygonId),
);
}