使用库:amap_location
在pubspec.yaml中导入:
dependencies:
#高德地图
amap_location: ^0.2.0
#权限检查 需要iOS9.3以上版本
# simple_permissions: ^0.1.9
示例代码:
void main() => runApp(WSAMap());
import 'package:flutter/material.dart';
//高德地图
import 'package:amap_location/amap_location.dart';
class WSAMap extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: WSAMapPage(title: 'Flutter Demo Home Page'),
);
}
}
class WSAMapPage extends StatefulWidget {
WSAMapPage({Key key, this.title}) : super(key: key);
final String title;
@override
WSAMapPageState createState() => WSAMapPageState();
}
class WSAMapPageState extends State<WSAMapPage> {
int _counter = 0;
bool once = true;
@override
void initState() {
super.initState();
AMapLocationClient.setApiKey("3fa98dfeaf2f5156f053102a434c99ab");
}
_incrementCounter() async {
if (once) {
try {
await AMapLocationClient.startup(new AMapLocationOption(
desiredAccuracy:
CLLocationAccuracy.kCLLocationAccuracyHundredMeters));
// await AMapLocationClient.getLocation(true);
AMapLocationClient.onLocationUpate.listen((AMapLocation loc) {
if (!mounted) return;
print(loc);
});
AMapLocationClient.startLocation();
} catch (e) {
print(e);
}
once = false;
}
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed PATHText $_counter the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
网友评论