amap_location_plugin 0.2.0 copy "amap_location_plugin: ^0.2.0" to clipboard
amap_location_plugin: ^0.2.0 copied to clipboard

A new Flutter Location plugin based on Amap SDK.

example/lib/main.dart

import 'package:amap_location_plugin/amap_location_plugin.dart';
import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';

void main() => runApp(new MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _location = 'Unknown';
  AmapLocation _amapLocation = AmapLocation();
  StreamSubscription<String> _locationSubscription;

  @override
  void initState() {
    super.initState();
    _locationSubscription = _amapLocation.onLocationChanged.listen((String location) {
      print(location);
      if (!mounted) return;
      setState(() {
        _location = location;
      });
    });
//    initPlatformState();
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> getLocation() async {
    String location;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      location = await _amapLocation.getLocation;
      print(location);
    } on PlatformException {
      location = 'Failed to get location.';
    }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _location = location;
    });
  }

  Future<void> startLocation() async {
    await _amapLocation.startLocation;
  }

  Future<void> stopLocation() async {
    await _amapLocation.stopLocation;
  }

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      home: new Scaffold(
        appBar: new AppBar(
          title: const Text('Plugin example app'),
        ),
        body: new Center(
          child: Column(
            children: <Widget>[
              new FlatButton(onPressed: startLocation, child: Text("开始定位")),
              new FlatButton(onPressed: stopLocation, child: Text("停止定位")),
              new Text('Running on: $_location\n'),
            ],
          ),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    if (_locationSubscription != null) {
      _locationSubscription.cancel();
    }
  }
}
0
likes
30
pub points
0%
popularity

Publisher

unverified uploader

A new Flutter Location plugin based on Amap SDK.

Repository (GitHub)
View/report issues

License

Apache-2.0 (LICENSE)

Dependencies

flutter

More

Packages that depend on amap_location_plugin