-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.dart
118 lines (107 loc) · 3.34 KB
/
main.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
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_z_location/flutter_z_location.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
double latitude = 0;
double longitude = 0;
String geocodeStr = '';
String ipInfo = 'ip: ,\ngeocode: ';
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
String platformVersion;
try {
platformVersion = await FlutterZLocation.getPlatformVersion() ?? 'Unknown platform version';
} on PlatformException {
platformVersion = 'Failed to get platform version.';
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
children: [
const SizedBox(height: 12),
Text('Running on: $_platformVersion\n'),
const SizedBox(height: 12),
OutlinedButton(
onPressed: () async {
Permission.location.request().then((value) async {
final res = await FlutterZLocation.getCoordinate(accuracy: 1);
debugPrint(res.toString());
setState(() {
latitude = res.latitude;
longitude = res.longitude;
geocodeCoordinate();
});
});
},
child: const Text('getCoordinate'),
),
const SizedBox(height: 12),
Text(
'latitude: $latitude \nlongitude: $longitude \ngeocode:$geocodeStr',
style: const TextStyle(fontSize: 15, color: Colors.black),
),
OutlinedButton(
onPressed: () async {
// 获取ip地址
final ipStr = await FlutterZLocation.getIp();
// ip反编码出地址
final res = await FlutterZLocation.geocodeIp(
ipStr,
pathHead: 'assets/',
hasGetCoordinate: true,
);
debugPrint(ipStr);
debugPrint(res.toString());
setState(() {
ipInfo = 'ip: $ipStr,\ngeocode: ${res.address}';
});
},
child: const Text('get ip'),
),
Text(
ipInfo,
style: const TextStyle(fontSize: 15, color: Colors.black),
),
],
),
),
),
);
}
Future<void> geocodeCoordinate() async {
final res = await FlutterZLocation.geocodeCoordinate(latitude, longitude);
if (kDebugMode) {
print(res);
}
setState(() {
geocodeStr = res.address;
});
}
}