| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import 'dart:async';
- import 'dart:convert';
- import 'package:amap_location/location_option.dart';
- import 'package:amap_location/location_result_entity.dart';
- import 'package:flutter/services.dart';
- class AmapLocation {
- static const MethodChannel _channel = const MethodChannel('amap_location');
- static final AmapLocation _instance = AmapLocation();
- static AmapLocation get instance => _instance;
- StreamController _streamController;
- Stream<LocationResultEntity> get locationStream => _streamController.stream;
- static Future<String> get platformVersion async {
- final String version = await _channel.invokeMethod('getPlatformVersion');
- return version;
- }
- Future<void> startLocation({LocationOption options}) async {
- _setLocationCallback();
- return await _channel.invokeMethod(
- 'startLocation', options?.toMap() ?? LocationOption().toMap());
- }
- Future<void> disposed() async {
- if (!_streamController.isClosed) {
- await _streamController.close();
- }
- _streamController = null;
- return await _channel.invokeMethod("closeLocation");
- }
- void _setLocationCallback() {
- if (_streamController == null) {
- _streamController = StreamController<LocationResultEntity>.broadcast();
- }
- _channel.setMethodCallHandler(platformCallHandler);
- }
- Future<void> platformCallHandler(MethodCall call) async {
- try {
- if (call.method == "location") {
- LocationResultEntity entity =
- LocationResultEntity().fromJson(jsonDecode(call.arguments));
- _streamController.add(entity);
- }
- } catch (ex) {
- print('Unexpected error: $ex');
- }
- }
- }
|