美文网首页Flutter
Flutter - 高德地图

Flutter - 高德地图

作者: 神灬渐入嘉靜 | 来源:发表于2020-02-13 15:16 被阅读0次

使用库: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),
      ),
    );
  }
}

相关文章

网友评论

    本文标题:Flutter - 高德地图

    本文链接:https://www.haomeiwen.com/subject/nsxlfhtx.html