-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_keyword_page.dart
150 lines (136 loc) · 4.65 KB
/
search_keyword_page.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
import 'package:http/http.dart' as http;
import 'package:mapbox_gl/mapbox_gl.dart';
import 'class_definition.dart';
import 'util_common.dart';
class SearchKeywordPage extends StatefulWidget {
const SearchKeywordPage({Key? key}) : super(key: key);
@override
SearchKeywordPageState createState() => SearchKeywordPageState();
}
// 地名
class PlaceName {
String title;
int titleLength;
PrefMuni prefMuni;
LatLng latLng;
PlaceName(this.title, this.titleLength, this.prefMuni, this.latLng);
}
class SearchKeywordPageState extends State<SearchKeywordPage> {
List<PlaceName> _placeList = [];
Map<int, PrefMuni> _prefMuniMap = {};
@override
Widget build(BuildContext context) {
final args =
ModalRoute.of(context)!.settings.arguments as FullSearchKeyword;
_prefMuniMap = args.prefMuniMap;
return Scaffold(
appBar: PreferredSize(
preferredSize: const Size.fromHeight(60.0),
child: AppBar(
title: TextField(
autofocus: true,
decoration: const InputDecoration(
prefixIcon: Icon(Icons.search),
hintText: '地名検索',
fillColor: Colors.white,
filled: true,
border: InputBorder.none,
),
onSubmitted: (value) async => {_keywordChangeAndViewList(value)}),
),
),
body: _makeDisplayForm(),
);
}
// 表示フォームウィジェット
Widget _makeDisplayForm() {
return Container(
padding: const EdgeInsets.symmetric(horizontal: 20.0),
child: Align(
alignment: Alignment.topLeft,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Gap(12),
Flexible(
child: ListView.builder(
itemCount: _placeList.length,
itemBuilder: (BuildContext context, int index) {
return _placeInfoItem(_placeList[index]);
},
),
),
],
),
),
);
}
// 項目表示ウィジェット
Widget _placeInfoItem(PlaceName placeName) {
final String title = formatLabel(placeName.title, 13);
final String prefMuniText =
'${placeName.prefMuni.prefecture}${placeName.prefMuni.municipalities}';
return Card(
child: Container(
decoration: BoxDecoration(
border: Border.all(width: 1.0, color: Colors.blue),
),
child: ListTile(
leading: (const Icon(
Icons.not_listed_location,
size: 30.0,
)),
title: Text(title),
subtitle: Text(prefMuniText),
onTap: () {
Navigator.pop(context,
LatLng(placeName.latLng.latitude, placeName.latLng.longitude));
},
),
),
);
}
// キーワードを変更して一覧を表示
Future<void> _keywordChangeAndViewList(keyword) async {
final List<PlaceName> placeList = await _searchPlaceName(keyword);
setState(() {
_placeList = placeList;
});
}
// 地名から緯度経度を取得
Future<List<PlaceName>> _searchPlaceName(String keyword) async {
final List<PlaceName> placeList = [];
if (keyword == '') {
return placeList;
}
final String geoJson = await _getGeo(keyword);
final List<dynamic> geoResultList = jsonDecode(geoJson);
for (int i = 0; i < geoResultList.length; i++) {
final Map<String, dynamic> geoResult = geoResultList[i];
final title = geoResult['properties']['title'];
final String muniCode = geoResult['properties']['addressCode'];
final String prefecture =
(muniCode == '' ? '' : _prefMuniMap[int.parse(muniCode)]!.prefecture);
final String municipalities = (muniCode == ''
? ''
: _prefMuniMap[int.parse(muniCode)]!.municipalities);
final PrefMuni prefMuni = PrefMuni(prefecture, municipalities);
final double latitude = geoResult['geometry']['coordinates'][1];
final double longitude = geoResult['geometry']['coordinates'][0];
final LatLng latLng = LatLng(latitude, longitude);
PlaceName placeName = PlaceName(title, title.length, prefMuni, latLng);
placeList.add(placeName);
}
placeList.sort((a, b) => a.titleLength.compareTo(b.titleLength));
return placeList;
}
// キーワードをジオコーディング
Future<String> _getGeo(String keyword) async {
return await http.read(Uri.parse(
'https://msearch.gsi.go.jp/address-search/AddressSearch?q=$keyword'));
}
}