| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- import 'dart:async';
- import 'dart:convert';
- import 'dart:io';
- import 'package:flutter/services.dart';
- import 'package:flutter/material.dart';
- import 'amap_marker.dart';
- import 'location_option.dart';
- import 'location_result_entity.dart';
- // IOS:
- // add info.plist GaoDeAppKey
- class AmapLocation {
- static const MethodChannel _channel = const MethodChannel('amap_location');
- static final AmapLocation _instance = AmapLocation();
- static const String viewType = "com.i2edu.mapView";
- static AmapLocation get instance => _instance;
- StreamController _locationStreamController;
- StreamController _guideStreamController;
- Stream<LocationResultEntity> get locationStream => _locationStreamController.stream;
- Stream<Map> get guideStream => _guideStreamController.stream;
- AmapLocation() {
- _channel.setMethodCallHandler(platformCallHandler);
- _locationStreamController = StreamController<LocationResultEntity>.broadcast();
- _guideStreamController = StreamController<Map>.broadcast();
- }
- static Future<String> get platformVersion async {
- final String version = await _channel.invokeMethod('getPlatformVersion');
- return version;
- }
- static Future<void> registerView() async {
- return await _channel.invokeMethod('registerView');
- }
- Widget buildMapView(VoidCallback onPlatformViewCreated) =>
- Platform.isAndroid
- ? AndroidView(
- viewType: viewType,
- creationParams: {},
- creationParamsCodec: const StandardMessageCodec(),
- onPlatformViewCreated: (id) => onPlatformViewCreated(),
- ) : UiKitView(
- viewType: viewType,
- creationParams: {},
- creationParamsCodec: const StandardMessageCodec(),
- onPlatformViewCreated: (id) => onPlatformViewCreated(),
- );
- Future<void> setMapMarkers(List<AmapMarker> markers) async {
- return await _channel.invokeMethod("setMarkers", {"markers": markers.map((f) => f.toMap()).toList()});
- }
- Future<void> onCreateMapView() async {
- return await _channel.invokeMethod('onCreate');
- }
- Future<void> onPauseMapView() async {
- return await _channel.invokeMethod('onPause');
- }
- Future<void> onResumeMapView() async {
- return await _channel.invokeMethod('onResume');
- }
- Future<void> startLocation({LocationOption options}) async {
- if (_locationStreamController == null) {
- _locationStreamController = StreamController<LocationResultEntity>.broadcast();
- }
- return await _channel.invokeMethod(
- 'startLocation', options?.toMap() ?? LocationOption().toMap());
- }
- Future<void> disposed() async {
- if (!_locationStreamController.isClosed) {
- await _locationStreamController.close();
- }
- _locationStreamController = null;
- return await _channel.invokeMethod("closeLocation");
- }
- Future<void> disposedMapView() async {
- if (!_guideStreamController.isClosed) {
- await _guideStreamController.close();
- }
- _guideStreamController = null;
- return;
- }
- Future<void> platformCallHandler(MethodCall call) async {
- try {
- if (call.method == "location") {
- LocationResultEntity entity =
- LocationResultEntity().fromJson(jsonDecode(call.arguments));
- _locationStreamController.add(entity);
- } else if (call.method == "guide") {
- _guideStreamController.add(call.arguments);
- }
- } catch (ex) {
- print('Unexpected error: $ex');
- }
- return null;
- }
- }
|