Newer
Older
flutterBaseApp / lib / utils / map_utils.dart
StephanieGitHub on 9 Feb 2021 2 KB first commit
import 'dart:math';

import 'package:amap_map_fluttify/amap_map_fluttify.dart';

/// 地图工具类
class MapUtils {
  ///计算两点距离
  ///[start] 第一个点坐标[lng,lat]
  ///[end]第二个点坐标[lng,lat]
  static double calculateDistance(start, end) {
    if ((start != null) || (end != null)) {
      double d1 = 0.01745329251994329;
      double d2 = start[0]; //lng
      double d3 = start[1]; //lat
      double d4 = end[0]; //lng
      double d5 = end[1]; //lat
      d2 *= d1;
      d3 *= d1;
      d4 *= d1;
      d5 *= d1;
      double d6 = sin(d2);
      double d7 = sin(d3);
      double d8 = cos(d2);
      double d9 = cos(d3);
      double d10 = sin(d4);
      double d11 = sin(d5);
      double d12 = cos(d4);
      double d13 = cos(d5);
      List<double> arrayOfDouble1 = List<double>(3);
      List<double> arrayOfDouble2 = List<double>(3);
      arrayOfDouble1[0] = (d9 * d8);
      arrayOfDouble1[1] = (d9 * d6);
      arrayOfDouble1[2] = d7;
      arrayOfDouble2[0] = (d13 * d12);
      arrayOfDouble2[1] = (d13 * d10);
      arrayOfDouble2[2] = d11;
      double d14 = sqrt((arrayOfDouble1[0] - arrayOfDouble2[0]) *
              (arrayOfDouble1[0] - arrayOfDouble2[0]) +
          (arrayOfDouble1[1] - arrayOfDouble2[1]) *
              (arrayOfDouble1[1] - arrayOfDouble2[1]) +
          (arrayOfDouble1[2] - arrayOfDouble2[2]) *
              (arrayOfDouble1[2] - arrayOfDouble2[2]));
      print((asin(d14 / 2.0) * 12742001.579854401));
      return (asin(d14 / 2.0) * 12742001.579854401).abs();
    } else {
      return 0.0;
    }
  }
}

/// 获取随机坐标
mixin NextLatLng {
  final random = Random();

  LatLng getNextLatLng({LatLng center}) {
    center ??= LatLng(39.90960, 116.397228);
    return LatLng(
      center.latitude + random.nextDouble(),
      center.longitude + random.nextDouble(),
    );
  }

  List<LatLng> getNextBatchLatLng(int count) {
    return [
      for (int i = 0; i < count; i++)
        LatLng(
          39.90960 + random.nextDouble() * 4,
          116.397228 + random.nextDouble() * 4,
        )
    ];
  }
}