attach method

void attach(
  1. int viewId
)

Implementation

void attach(int viewId) {
  if (_disposed) return;

  _channel = MethodChannel('heyhip_amap_map_$viewId');

  // 接收事件
  _channel!.setMethodCallHandler((call) async {
    // 开始
    switch (call.method) {
      case 'onMapLoaded':
        markMapReady();
            // ✅ 通知外部:地图真正 ready
          _onMapLoaded?.call();
        break;

      case 'onMarkerClick':
        if (call.arguments is! Map) return;
        final map = Map<String, dynamic>.from(call.arguments);
        final markerId = map['markerId'] as String;
        final lat = map['latitude'] as double;
        final lng = map['longitude'] as double;

        _onMarkerClick?.call(markerId, LatLng(lat, lng));
        break;

      case 'onMapClick':
        if (call.arguments is! Map) return;
        final map = Map<String, dynamic>.from(call.arguments);
        final lat = map['latitude'] as double;
        final lng = map['longitude'] as double;

        _onMapClick?.call(LatLng(lat, lng));
        break;

      case 'onCameraIdle':
        if (call.arguments is! Map) return;
        final map = Map<String, dynamic>.from(call.arguments);
        _onCameraIdle?.call(CameraPosition.fromMap(map));
        break;

      case 'onCameraMove':
        if (call.arguments is! Map) return;
        final map = Map<String, dynamic>.from(call.arguments);
        _onCameraMove?.call(CameraPosition.fromMap(map));
        break;

      case 'onCameraMoveStart':
        if (call.arguments is! Map) return;
        final map = Map<String, dynamic>.from(call.arguments);
        _onCameraMoveStart?.call(CameraPosition.fromMap(map));
        break;

      case 'onMarkerPopupToggle':
        if (call.arguments is! Map) return;
        final map = Map<String, dynamic>.from(call.arguments);

        final markerId = map['markerId'] as String?;
        final action = map['action'] as String?;

        if (markerId != null && action != null) {
          final isOpen = action == 'open';
          final latitude = (map['latitude'] as num?)?.toDouble();
          final longitude = (map['longitude'] as num?)?.toDouble();

          _onMarkerPopupToggle?.call(markerId, isOpen, latitude, longitude);
        }
        break;

      default:
        debugPrint('未知 native 方法: ${call.method}');
    }
  });

}