Flutter地理编码插件google_geocoder_krutus的使用
Flutter地理编码插件google_geocoder_krutus的使用
更新说明
增加了对核心结果中数据的支持。现在可以从每个addressComponents
中的每个results
数据中获取格式化后的所有类型数据。
- establishment
- plusCode
- premise
- subLocalityLevel1
- subLocalityLevel2
- subLocality
- locality
- administrativeAreas1
- administrativeAreas2
- countries
- postalCodes
例如:
print(addresses!.establishment);
描述
这是一个使用Google Maps Geocoder API进行地理编码或逆地理编码的Flutter插件。
特性
正向地理编码
使用Google Maps Geocoder API将街道或地址字符串转换为相关的地址数据。
逆向地理编码
使用Google Maps Geocoder API将坐标转换为相关的地址数据。
地址ID地理编码
使用Google Maps Geocoder API将地址ID转换为相关的地址数据。
使用方法
导入package:google_geocoder_krutus/google_geocoder_krutus.dart
,并使用GoogleGeocoderKrutus
访问设备系统提供的地理编码服务。
示例:
GeocoderResponse? reverseGeocode = await GoogleGeocoderKrutus.reverseGeoCode(
apiKey: '<GOOGLE MAPS API KEY>',
coordinates: Coordinates(latitude: 0, longitude: 0),
);
GeocoderResponse? addressQuery = await GoogleGeocoderKrutus.addressQuery(
apiKey: '<GOOGLE MAPS API KEY>',
address: '<ADDRESS STRING TO QUERY>',
);
GeocoderResponse? placeID = await GoogleGeocoderKrutus.placeID(
apiKey: '<GOOGLE MAPS API KEY>',
placeId: '<PLACE ID TO QUERY>',
);
完整示例代码
import 'package:flutter/material.dart';
import 'package:google_geocoder_krutus/google_geocoder_krutus.dart';
import 'package:google_geocoder_krutus/models.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// 这个小部件是你的应用的根。
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
[@override](/user/override)
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
void _incrementCounter() async {
var addresses = await GoogleGeocoderKrutus.reverseGeoCode(
apiKey: '<API KEY>',
coordinates: Coordinates(latitude: 28.612912, longitude: 77.227321),
);
print(addresses!.establishment);
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'你已经按下了按钮多少次:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: '增加',
child: const Icon(Icons.add),
),
);
}
}
更多关于Flutter地理编码插件google_geocoder_krutus的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter地理编码插件google_geocoder_krutus的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,下面是一个关于如何使用Flutter地理编码插件google_geocoder_krutus
的代码示例。这个插件允许你将地理坐标(经纬度)转换为地址,或者将地址转换为地理坐标。
首先,你需要在你的Flutter项目的pubspec.yaml
文件中添加这个插件的依赖:
dependencies:
flutter:
sdk: flutter
google_geocoder_krutus: ^最新版本号 # 请替换为实际可用的最新版本号
然后运行flutter pub get
来安装依赖。
接下来,你可以在你的Dart文件中使用这个插件。以下是一个简单的示例,展示了如何将地址转换为地理坐标(反向地理编码)和将地理坐标转换为地址(正向地理编码)。
import 'package:flutter/material.dart';
import 'package:google_geocoder_krutus/google_geocoder_krutus.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String address = '';
String latitude = '';
String longitude = '';
String formattedAddress = '';
void _geocodeAddress() async {
final Geocoder geocoder = Geocoder();
try {
List<GeocodingResult> results = await geocoder.geocodeAddress(address);
if (results.isNotEmpty) {
GeocodingResult result = results[0];
setState(() {
latitude = result.geometry.location.lat.toString();
longitude = result.geometry.location.lng.toString();
formattedAddress = result.formattedAddress;
});
}
} catch (e) {
print("Error geocoding address: $e");
}
}
void _reverseGeocodeCoordinates() async {
final Geocoder geocoder = Geocoder();
try {
List<GeocodingResult> results = await geocoder.reverseGeocode(
double.parse(latitude),
double.parse(longitude)
);
if (results.isNotEmpty) {
GeocodingResult result = results[0];
setState(() {
formattedAddress = result.formattedAddress;
});
}
} catch (e) {
print("Error reverse geocoding coordinates: $e");
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Google Geocoder Krutus Example'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
TextField(
decoration: InputDecoration(labelText: 'Address'),
onChanged: (value) => setState(() => address = value),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () => _geocodeAddress(),
child: Text('Geocode Address'),
),
SizedBox(height: 16),
TextField(
decoration: InputDecoration(labelText: 'Latitude'),
keyboardType: TextInputType.number,
onChanged: (value) => setState(() => latitude = value),
),
SizedBox(height: 8),
TextField(
decoration: InputDecoration(labelText: 'Longitude'),
keyboardType: TextInputType.number,
onChanged: (value) => setState(() => longitude = value),
),
SizedBox(height: 16),
ElevatedButton(
onPressed: () => _reverseGeocodeCoordinates(),
child: Text('Reverse Geocode Coordinates'),
),
SizedBox(height: 16),
Text('Formatted Address: $formattedAddress'),
SizedBox(height: 16),
Text('Latitude: $latitude'),
SizedBox(height: 8),
Text('Longitude: $longitude'),
],
),
),
),
);
}
}
在这个示例中,我们创建了一个简单的Flutter应用,它有两个主要功能:
- 正向地理编码:输入一个地址,然后将其转换为地理坐标(纬度和经度)。
- 反向地理编码:输入纬度和经度,然后将其转换为格式化的地址。
请注意,google_geocoder_krutus
插件可能依赖于Google Maps Geocoding API,因此你可能需要配置API密钥并确保你的应用有权访问该API。请查阅插件的官方文档以获取更多信息和配置指南。