hs_amap_map 1.0.0 copy "hs_amap_map: ^1.0.0" to clipboard
hs_amap_map: ^1.0.0 copied to clipboard

A Flutter plugin for AMap (AutoNavi) maps. Supports 2D/3D map rendering, marker customization, camera position control, and zoom/rotation gestures for both iOS and Android.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:hs_amap_map/hs_amap_map.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await HsAmapMap.setPrivacyAgreed(true);
  await HsAmapMap.setApiKey(androidKey: '你的Key', iosKey: '你的iOS AppKey');
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('HS AMap Test')),
        body: const MapPage(),
      ),
    );
  }
}

class MapPage extends StatefulWidget {
  const MapPage({super.key});
  @override
  State<MapPage> createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  HSAmapController? _controller;
  String _status = '等待地图创建...';
  bool _controllerReady = false;

  static const HSLatLng hangzhouCenter = HSLatLng(30.25, 120.16);

  @override
  void initState() {
    super.initState();
    // 如果 5 秒后 controller 仍为 null,说明地图创建失败
    Future.delayed(const Duration(seconds: 5), () {
      if (!_controllerReady && mounted) {
        setState(() {
          _status = '错误:地图控制器未就绪,请检查 Key 与原生配置';
        });
      }
    });
  }

  void _moveToHangzhou() {
    debugPrint('按钮点击,_controller = $_controller');
    if (_controller == null) {
      setState(() => _status = '控制器未初始化,无法移动');
      return;
    }
    _controller!.animateCamera(HSCameraUpdate.newLatLngZoom(hangzhouCenter, 12), durationMs: 500);
    setState(() => _status = '程序正在移动到杭州...');
  }

  void _addMarker() async {
    if (_controller == null) return;
    final markers = {
      // 默认图标(不设置 icon)
      HSMarker(id: 'default_marker', position: HSLatLng(39.9, 116.4), title: '默认点', animationType: HSMarkerAnimationType.grow, animationDuration: 500),
      // 网络图标
      HSMarker(id: 'net_marker', position: HSLatLng(39.91, 116.4), title: '网络图标', icon: HSBitmapDescriptor.fromNetwork('https://img.icons8.com/color/48/marker.png'), animationType: HSMarkerAnimationType.grow, animationDuration: 500),
      // Asset 图标(需异步加载)
      HSMarker(id: 'asset_marker', position: HSLatLng(39.89, 116.4), title: '本地图标', icon: await HSBitmapDescriptor.fromAsset('assets/map_pin.png', scale: 1.5), animationType: HSMarkerAnimationType.drop),
    };
    print('更新 markers: ${markers.length}');
    _controller!.updateMarkers(markers);
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Expanded(
          child: SizedBox(
            height: 400,
            child: HSAmapView(
              showCompass: false,
              showMyLocation: true,
              showZoomControls: false,
              initialCameraPosition: const HSCameraPosition(target: HSLatLng(39.9, 116.4), zoom: 10),
              onMapCreated: (controller) {
                debugPrint('✅ onMapCreated 回调触发,获取到控制器');
                _controller = controller;
                _controllerReady = true;
                setState(() => _status = '地图就绪,可操作');
              },
              onCameraMoveStarted: (reason) {
                setState(() {
                  _status = '相机移动开始: ${reason == HSCameraMoveReason.gesture ? "手势拖拽" : "程序调用"}';
                });
              },
              onMarkerClick: (id) => print('Clicked: $id'),
              onCameraMoveEnded: (position, reason) {
                setState(() {
                  _status =
                      '相机移动结束: ${reason == HSCameraMoveReason.gesture ? "手势拖拽" : "程序调用"} '
                      '中心(${position.target.latitude.toStringAsFixed(4)}, ${position.target.longitude.toStringAsFixed(4)})';
                });
              },
            ),
          ),
        ),
        Padding(
          padding: const EdgeInsets.all(12),
          child: ElevatedButton(onPressed: _moveToHangzhou, child: const Text('移动到杭州')),
        ),
        Padding(
          padding: const EdgeInsets.all(12),
          child: ElevatedButton(onPressed: _addMarker, child: const Text('添加marker')),
        ),
        Container(
          width: double.infinity,
          height: 100,
          padding: const EdgeInsets.all(12),
          color: Colors.grey[200],
          child: Text(_status, style: const TextStyle(fontSize: 16)),
        ),
      ],
    );
  }
}
0
likes
0
points
22
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin for AMap (AutoNavi) maps. Supports 2D/3D map rendering, marker customization, camera position control, and zoom/rotation gestures for both iOS and Android.

Repository

License

unknown (license)

Dependencies

flutter

More

Packages that depend on hs_amap_map

Packages that implement hs_amap_map