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'; // IOS: // add info.plist GaoDeAppKey class AmapLocation { static const MethodChannel _channel = const MethodChannel('amap_location'); static final AmapLocation _instance = AmapLocation(); static AmapLocation get instance => _instance; StreamController _streamController; Stream get locationStream => _streamController.stream; AmapLocation(){ _channel.setMethodCallHandler(platformCallHandler); _streamController = StreamController.broadcast(); } static Future get platformVersion async { final String version = await _channel.invokeMethod('getPlatformVersion'); return version; } Future startLocation({LocationOption options}) async { if (_streamController == null) { _streamController = StreamController.broadcast(); } return await _channel.invokeMethod( 'startLocation', options?.toMap() ?? LocationOption().toMap()); } Future disposed() async { if (!_streamController.isClosed) { await _streamController.close(); } _streamController = null; return await _channel.invokeMethod("closeLocation"); } Future 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'); } return null; } }