Skip to content

Commit

Permalink
feat: add reverse geocoding #16
Browse files Browse the repository at this point in the history
  • Loading branch information
okodeee committed Dec 3, 2023
1 parent 7ebbed5 commit bdd5a85
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 43 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kakao_map">

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<application
Expand Down
168 changes: 125 additions & 43 deletions lib/view/writing_view.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import 'dart:io';

import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as p;
import 'package:path_provider/path_provider.dart';

import 'package:native_exif/native_exif.dart';
import 'package:http/http.dart' as http;

import 'package:kpostal/kpostal.dart';


class WritingView extends StatefulWidget {
const WritingView({super.key});

Expand All @@ -13,31 +20,101 @@ class WritingView extends StatefulWidget {
}

class _WritingViewState extends State<WritingView> {
String gpsApiKey = '';

DateTime? selectedDate;
TextEditingController titleController = TextEditingController();
TextEditingController contentController = TextEditingController();

XFile? _image; //이미지를 담을 변수 선언
final ImagePicker picker = ImagePicker(); //ImagePicker 초기화

//이미지를 가져오는 함수
Future getImage(ImageSource imageSource) async {
//pickedFile에 ImagePicker로 가져온 이미지가 담긴다.
final XFile? pickedFile = await picker.pickImage(source: imageSource);
if (pickedFile != null) {
final picker = ImagePicker();

XFile? pickedFile;
Exif? exif;
Map<String, Object>? attributes;
DateTime? shootingDate;
ExifLatLong? coordinates;

@override
void initState() {
super.initState();
}

Future<void> showError(Object e) async {
debugPrintStack(label: e.toString(), stackTrace: e is Error ? e.stackTrace : null);

return showDialog<void>(
context: context,
builder: (context) {
return AlertDialog(
title: const Text('Error'),
content: SingleChildScrollView(
child: ListBody(
children: [
Text(e.toString()),
],
),
),
actions: <Widget>[
TextButton(
child: const Text('OK'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}

setState(() {
_image = XFile(pickedFile.path); //가져온 이미지를 _image에 저장
});
Future getImage() async {
pickedFile = await picker.pickImage(source: ImageSource.gallery);
if (pickedFile == null) {
return;
}

exif = await Exif.fromPath(pickedFile!.path);
attributes = await exif!.getAttributes();
shootingDate = await exif!.getOriginalDate();
coordinates = await exif!.getLatLong();

print(attributes);
print(shootingDate);
print(coordinates);

final gpsUrl =
'https://maps.googleapis.com/maps/api/geocode/json?latlng=${coordinates!.latitude},${coordinates!.longitude}&key=$gpsApiKey&language=ko';
final responseGps = await http.get(Uri.parse(gpsUrl));
final formatted_address = jsonDecode(responseGps.body)['results'][0]['formatted_address'];
ETALocation = jsonDecode(responseGps.body)['results'][0]['address_components'][2]['long_name'];
print(ETALocation);


setState(() {
selectedDate = shootingDate;
address = formatted_address;
});
}

Future closeImage() async {
await exif?.close();
shootingDate = null;
attributes = {};
exif = null;
coordinates = null;

setState(() {});
}


String postCode = '';
String address = '';
String latitude = '';
String longitude = '';
String kakaoLatitude = '';
String kakaoLongitude = '';
String ETALocation = '';


@override
Expand All @@ -57,7 +134,7 @@ class _WritingViewState extends State<WritingView> {
icon: Icon(Icons.arrow_back_ios_rounded, color: Colors.black)),
),
body: GestureDetector(
onTap: (){
onTap: () {
FocusScope.of(context).unfocus();
},
child: SingleChildScrollView(
Expand All @@ -67,7 +144,6 @@ class _WritingViewState extends State<WritingView> {
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_buildPhotoArea(),

Text(
'날짜',
style: TextStyle(fontSize: 17.0, fontWeight: FontWeight.w700),
Expand Down Expand Up @@ -115,20 +191,24 @@ class _WritingViewState extends State<WritingView> {
child: ElevatedButton(
onPressed: () async {
await Navigator.push(context, MaterialPageRoute(
builder: (_) => KpostalView(
callback: (Kpostal result) {
print(result.sigungu);
setState(() {
this.postCode = result.postCode;
this.address = result.address;
this.latitude = result.latitude.toString();
this.longitude = result.longitude.toString();
this.kakaoLatitude = result.kakaoLatitude.toString();
this.kakaoLongitude =
result.kakaoLongitude.toString();
});
},
),
builder: (_) =>
KpostalView(
callback: (Kpostal result) {
setState(() {
this.postCode = result.postCode;
this.address = result.address;
this.latitude = result.latitude.toString();
this.longitude = result.longitude.toString();
this.kakaoLatitude =
result.kakaoLatitude.toString();
this.kakaoLongitude =
result.kakaoLongitude.toString();
this.ETALocation =
result.sigungu.toString();
});
print(ETALocation);
},
),
));
},
child: Text(
Expand Down Expand Up @@ -188,25 +268,27 @@ class _WritingViewState extends State<WritingView> {
),
);
}


Widget _buildPhotoArea() {
return _image != null
? Container(
width: 100,
height: 100,
child: Image.file(File(_image!.path)), //가져온 이미지를 화면에 띄워주는 코드
)
: Container(
return pickedFile == null
? Container(
width: 100,
height: 100,
child: IconButton(
onPressed: () {
getImage();
},
icon: Icon(Icons.camera_alt),
color: Color(0xFF8474F7),
// If atleast 1 images is selected
)
)
: Container(
width: 100,
height: 100,
child: IconButton(
onPressed: (){
getImage(ImageSource.camera);
},
icon: Icon(Icons.camera_alt),
color: Color(0xFF8474F7),
// size: 30,
),
child: Image.file(File(pickedFile!.path)),

);
}

}

0 comments on commit bdd5a85

Please sign in to comment.