import 'dart:math' show cos, pi;

import '../../flutter_map.dart';
import '../entity/latlng_bounds.dart';

class CameraUpdateUtil {
  /// 根据圆心和半径，计算外接矩形范围
  static LatLngBounds? getBounds(
      double? latitude, double? longitude, double? radius) {
    if (latitude == null || longitude == null || radius == null) {
      return null;
    }
    // 半径转经纬度差值
    final double latRadius = radius / 111000.0;
    final double lngRadius = radius / (111000.0 * cos(latitude * pi / 180));

    final LatLng southwest = LatLng(
      latitude: latitude - latRadius,
      longitude: longitude - lngRadius,
    );

    final LatLng northeast = LatLng(
      latitude: latitude + latRadius,
      longitude: longitude + lngRadius,
    );

    return LatLngBounds(
      southwest: southwest,
      northeast: northeast,
    );
  }
}
