step 12 : ubah dari nomitism ke google apis untuk address
This commit is contained in:
@@ -12,6 +12,11 @@ class Constant {
|
||||
static String bearerName = "absensi-sas";
|
||||
static String accountGoogle = "absensi-google-account";
|
||||
|
||||
// api key google map
|
||||
static String apikeyGoogleMap = "AIzaSyCiN7EeJsUpXVLQKFfrj3sE5OTKebjpzek";
|
||||
static String baseUrlGoogleMapApis =
|
||||
"https://maps.googleapis.com/maps/api/geocode/json?";
|
||||
|
||||
static double designHeightPhone = 844;
|
||||
static double designWidthPhone = 390;
|
||||
|
||||
|
||||
41
lib/app/googleapis_location.dart
Normal file
41
lib/app/googleapis_location.dart
Normal file
@@ -0,0 +1,41 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:latlong2/latlong.dart';
|
||||
|
||||
Future<String> searchPosition(LatLng position) async {
|
||||
const apiKey = "AIzaSyAVUr4Ku4O1HlSkK8n9KGnUyqvsXBL-yfs";
|
||||
final latitudeString = position.latitude.toString();
|
||||
final longitudeString = position.longitude.toString();
|
||||
const url = 'https://maps.googleapis.com/maps/api/geocode/json';
|
||||
|
||||
try {
|
||||
final response = await Dio().get(
|
||||
url,
|
||||
queryParameters: {
|
||||
'latlng': '$latitudeString,$longitudeString',
|
||||
'key': apiKey,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
final data = response.data;
|
||||
// Mengambil compound_code dari hasil JSON
|
||||
final plusCode = data['plus_code'];
|
||||
if (plusCode != null && plusCode['compound_code'] != null) {
|
||||
return plusCode['compound_code'];
|
||||
} else {
|
||||
print('Alamat Tidak Ditemukan');
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
print('Failed to load data');
|
||||
return "";
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> positionToAddressGoogleApis(LatLng position) async {
|
||||
return await searchPosition(position);
|
||||
}
|
||||
@@ -63,8 +63,8 @@ class MyApp extends StatelessWidget {
|
||||
),
|
||||
|
||||
// home: TestMap(),
|
||||
// initialRoute: loginRoute,
|
||||
home: TestFlutterWebMap(),
|
||||
initialRoute: loginRoute,
|
||||
// home: TestFlutterWebMap(),
|
||||
// initialRoute: testFlutterMapRoute,
|
||||
onGenerateRoute: AppRoute.generateRoute,
|
||||
);
|
||||
|
||||
@@ -5,8 +5,8 @@ import 'base_repository.dart';
|
||||
class GoogleApisRepository extends BaseRepository {
|
||||
GoogleApisRepository({required super.graphql, required super.dio});
|
||||
|
||||
Future<String?> getAddressFromCoordinates(
|
||||
double latitude, double longitude) async {
|
||||
Future<String> getAddressFromCoordinates(
|
||||
String latitude, String longitude) async {
|
||||
final dio = Dio();
|
||||
const apiKey = "AIzaSyAVUr4Ku4O1HlSkK8n9KGnUyqvsXBL-yfs";
|
||||
final latitudeString = latitude.toString();
|
||||
@@ -31,15 +31,15 @@ class GoogleApisRepository extends BaseRepository {
|
||||
return plusCode['compound_code'];
|
||||
} else {
|
||||
print('Alamat Tidak Ditemukan');
|
||||
return null;
|
||||
return "";
|
||||
}
|
||||
} else {
|
||||
print('Failed to load data');
|
||||
return null;
|
||||
return "";
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error: $e');
|
||||
return null;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
75
lib/screen/home/googleapis_provider.dart
Normal file
75
lib/screen/home/googleapis_provider.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
|
||||
import 'package:absensi_sas/repository/googleapis_repository.dart';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../provider/dio_provider.dart';
|
||||
import '../../provider/graphql_provider.dart';
|
||||
import '../../repository/base_repository.dart';
|
||||
|
||||
abstract class GoogleApisProviderState extends Equatable {
|
||||
final DateTime date;
|
||||
const GoogleApisProviderState(this.date);
|
||||
@override
|
||||
List<Object?> get props => [date];
|
||||
}
|
||||
|
||||
class GoogleApisProviderStateInit extends GoogleApisProviderState {
|
||||
GoogleApisProviderStateInit() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class GoogleApisProviderStateLoading extends GoogleApisProviderState {
|
||||
GoogleApisProviderStateLoading() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class GoogleApisProviderStateError extends GoogleApisProviderState {
|
||||
final String message;
|
||||
GoogleApisProviderStateError({
|
||||
required this.message,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
class GoogleApisProviderStateDone extends GoogleApisProviderState {
|
||||
final String model;
|
||||
GoogleApisProviderStateDone({
|
||||
required this.model,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
//notifier
|
||||
class GoogleApisProviderNotifier extends StateNotifier<GoogleApisProviderState> {
|
||||
final Ref ref;
|
||||
GoogleApisProviderNotifier({
|
||||
required this.ref,
|
||||
}) : super(GoogleApisProviderStateInit());
|
||||
|
||||
void googleApisProvider(
|
||||
String latitude,
|
||||
String longitude,
|
||||
) async {
|
||||
try {
|
||||
state = GoogleApisProviderStateLoading();
|
||||
final graphql = ref.read(graphqlProvider(''));
|
||||
final dio = ref.read(dioProvider);
|
||||
final resp = await GoogleApisRepository(graphql: graphql, dio: dio)
|
||||
.getAddressFromCoordinates(
|
||||
latitude,
|
||||
longitude
|
||||
);
|
||||
state = GoogleApisProviderStateDone(model: resp);
|
||||
} catch (e) {
|
||||
if (e is BaseRepositoryException) {
|
||||
print(e.message);
|
||||
state = GoogleApisProviderStateError(message: e.message ?? "");
|
||||
} else {
|
||||
state = GoogleApisProviderStateError(message: e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// provider
|
||||
final googleApisProviderProvider =
|
||||
StateNotifierProvider<GoogleApisProviderNotifier, GoogleApisProviderState>(
|
||||
(ref) => GoogleApisProviderNotifier(ref: ref),
|
||||
);
|
||||
@@ -12,6 +12,7 @@ import 'package:location/location.dart';
|
||||
// import 'package:permission_handler/permission_handler.dart';
|
||||
import 'package:fluentui_system_icons/fluentui_system_icons.dart';
|
||||
|
||||
import '../../app/googleapis_location.dart';
|
||||
import '../../app/route.dart';
|
||||
import '../../provider/current_check_distance_provider.dart';
|
||||
import '../../provider/current_check_jam_presensi_provider.dart';
|
||||
@@ -128,10 +129,10 @@ class HomeScreen extends HookConsumerWidget {
|
||||
positionLongitude.value = _locationData.longitude.toString();
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
final address = await positionToAddress(
|
||||
final address = await positionToAddressGoogleApis(
|
||||
LatLng(_locationData.latitude!, _locationData.longitude!));
|
||||
|
||||
if (address.city != "") {
|
||||
if (address != "") {
|
||||
positionLatitude.value = _locationData.latitude.toString();
|
||||
positionLongitude.value = _locationData.longitude.toString();
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class GoogleApisNotifier extends StateNotifier<GoogleApisState> {
|
||||
GoogleApisNotifier({required this.ref}) : super(GoogleApisStateInit());
|
||||
|
||||
void getAddressGoogleApis(
|
||||
{required double latitude, required double longitude}) async {
|
||||
{required String latitude, required String longitude}) async {
|
||||
try {
|
||||
state = GoogleApisStateLoading();
|
||||
final graphql = ref.read(graphqlProvider(''));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'package:absensi_sas/app/googleapis_location.dart';
|
||||
import 'package:camera/camera.dart';
|
||||
import 'package:camera_web/camera_web.dart';
|
||||
import 'dart:io';
|
||||
@@ -94,16 +95,16 @@ class PresensiScreen extends HookConsumerWidget {
|
||||
_locationData = await location.getLocation();
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
final address = await positionToAddress(
|
||||
final address = await positionToAddressGoogleApis(
|
||||
LatLng(_locationData.latitude!, _locationData.longitude!));
|
||||
|
||||
if (positionLongitude.value.isEmpty && positionLatitude.value.isEmpty) {
|
||||
if (address.city != "") {
|
||||
if (address != "") {
|
||||
isLoadingAddressUserLocation.value = false;
|
||||
|
||||
ref.read(googleApisProvider.notifier).getAddressGoogleApis(
|
||||
latitude: _locationData.latitude!,
|
||||
longitude: _locationData.longitude!,
|
||||
latitude: _locationData.latitude.toString(),
|
||||
longitude: _locationData.longitude.toString(),
|
||||
);
|
||||
|
||||
// String address =
|
||||
|
||||
@@ -24,6 +24,7 @@ import 'package:shared_preferences/shared_preferences.dart';
|
||||
// import 'package:permission_handler/permission_handler.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../app/googleapis_location.dart';
|
||||
import '../../app/route.dart';
|
||||
import '../../provider/camera_controller_provider.dart';
|
||||
import '../../provider/current_check_distance_provider.dart';
|
||||
@@ -194,7 +195,7 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
_locationData = await location.getLocation();
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
final address = await positionToAddress(
|
||||
final address = await positionToAddressGoogleApis(
|
||||
LatLng(_locationData.latitude!, _locationData.longitude!));
|
||||
|
||||
// final address = await positionToAddress(
|
||||
@@ -203,12 +204,12 @@ class PresensiSelfieScreen extends HookConsumerWidget {
|
||||
|
||||
// Mendapatkan alamat dari posisi
|
||||
if (positionLongitude.value.isEmpty && positionLatitude.value.isEmpty) {
|
||||
if (address.city != "") {
|
||||
if (address != "") {
|
||||
isLoadingAddressUserLocation.value = false;
|
||||
|
||||
ref.read(googleApisProvider.notifier).getAddressGoogleApis(
|
||||
latitude: _locationData.latitude!,
|
||||
longitude: _locationData.longitude!,
|
||||
latitude: _locationData.latitude.toString(),
|
||||
longitude: _locationData.longitude.toString(),
|
||||
);
|
||||
|
||||
// String address =
|
||||
|
||||
Reference in New Issue
Block a user