petalmaps_ohos 0.0.1 copy "petalmaps_ohos: ^0.0.1" to clipboard
petalmaps_ohos: ^0.0.1 copied to clipboard

Flutter 花瓣地图(PetalMaps)插件,支持 HarmonyOS/OpenHarmony。调起地图首页、地点搜索、地点详情、路线规划、导航、打车等能力。

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:petalmaps_ohos/petalmaps_ohos.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: HomePage(),
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  String _platformVersion = 'Unknown';
  String _statusMessage = '';
  final _petalmapsOhosPlugin = PetalmapsOhos();

  @override
  void initState() {
    super.initState();
    initPlatformState();
  }

  Future<void> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await _petalmapsOhosPlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
    });
  }

  void _showStatus(String message) {
    setState(() {
      _statusMessage = message;
    });
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(message), duration: const Duration(seconds: 2)),
    );
  }

  /// 打开地图应用首页
  Future<void> _openMapHomePage() async {
    try {
      await _petalmapsOhosPlugin.openMapHomePage();
      _showStatus('已打开地图首页');
    } catch (e) {
      _showStatus('打开地图首页失败: $e');
    }
  }

  /// 打开地图应用进行地点搜索
  Future<void> _openMapTextSearch() async {
    try {
      await _petalmapsOhosPlugin.openMapTextSearch(
        destinationName: '云谷',
      );
      _showStatus('已打开地点搜索');
    } catch (e) {
      _showStatus('打开地点搜索失败: $e');
    }
  }

  /// 打开地图应用查看地点详情
  Future<void> _openMapPoiDetail() async {
    try {
      await _petalmapsOhosPlugin.openMapPoiDetail(
        latitude: 32.02065982629459,
        longitude: 118.788899213002,
        destinationPoiId: '563233191438217472',
      );
      _showStatus('已打开地点详情');
    } catch (e) {
      _showStatus('打开地点详情失败: $e');
    }
  }

  /// 打开地图应用规划路线
  Future<void> _openMapRoutePlan() async {
    try {
      await _petalmapsOhosPlugin.openMapRoutePlan(
        latitude: 31.983015468224288,
        longitude: 118.78058590757131,
      );
      _showStatus('已打开路线规划');
    } catch (e) {
      _showStatus('打开路线规划失败: $e');
    }
  }

  /// 打开地图应用进行导航
  Future<void> _openMapNavi() async {
    try {
      await _petalmapsOhosPlugin.openMapNavi(
        latitude: 31.983015468224288,
        longitude: 118.78058590757131,
      );
      _showStatus('已打开导航');
    } catch (e) {
      _showStatus('打开导航失败: $e');
    }
  }

  /// 打开地图应用打车页面
  Future<void> _openMapTaxi() async {
    try {
      await _petalmapsOhosPlugin.openMapTaxi(
        latitude: 31.983015468224288,
        longitude: 118.78058590757131,
      );
      _showStatus('已打开打车页面');
    } catch (e) {
      _showStatus('打开打车页面失败: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('PetalMaps 插件示例'),
      ),
      body: SafeArea(
        child: ListView(
          padding: const EdgeInsets.all(16.0),
          children: [
            Card(
              child: Padding(
                padding: const EdgeInsets.all(16.0),
                child: Text(
                  '运行平台: $_platformVersion',
                  style: const TextStyle(fontSize: 16),
                ),
              ),
            ),
            const SizedBox(height: 16),
            _buildFunctionButton(
              icon: Icons.home,
              title: '打开地图首页',
              subtitle: 'openMapHomePage',
              onTap: _openMapHomePage,
            ),
            _buildFunctionButton(
              icon: Icons.search,
              title: '地点搜索',
              subtitle: 'openMapTextSearch - 搜索"云谷"',
              onTap: _openMapTextSearch,
            ),
            _buildFunctionButton(
              icon: Icons.place,
              title: '查看地点详情',
              subtitle: 'openMapPoiDetail - 经纬度(32.02, 118.79)',
              onTap: _openMapPoiDetail,
            ),
            _buildFunctionButton(
              icon: Icons.directions,
              title: '路线规划',
              subtitle: 'openMapRoutePlan - 经纬度(31.98, 118.78)',
              onTap: _openMapRoutePlan,
            ),
            _buildFunctionButton(
              icon: Icons.navigation,
              title: '开始导航',
              subtitle: 'openMapNavi - 经纬度(31.98, 118.78)',
              onTap: _openMapNavi,
            ),
            _buildFunctionButton(
              icon: Icons.local_taxi,
              title: '打车',
              subtitle: 'openMapTaxi - 经纬度(31.98, 118.78)',
              onTap: _openMapTaxi,
            ),
            if (_statusMessage.isNotEmpty)
              Padding(
                padding: const EdgeInsets.only(top: 16.0),
                child: Card(
                  color: Colors.grey[100],
                  child: Padding(
                    padding: const EdgeInsets.all(12.0),
                    child: Text(
                      '状态: $_statusMessage',
                      style: const TextStyle(fontSize: 14),
                    ),
                  ),
                ),
              ),
          ],
        ),
      ),
    );
  }

  Widget _buildFunctionButton({
    required IconData icon,
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return Card(
      margin: const EdgeInsets.only(bottom: 8.0),
      child: ListTile(
        leading: Icon(icon, color: Colors.blue, size: 32),
        title: Text(title, style: const TextStyle(fontWeight: FontWeight.bold)),
        subtitle: Text(subtitle, style: const TextStyle(fontSize: 12)),
        trailing: const Icon(Icons.arrow_forward_ios, size: 16),
        onTap: onTap,
      ),
    );
  }
}
0
likes
115
points
64
downloads

Publisher

unverified uploader

Weekly Downloads

Flutter 花瓣地图(PetalMaps)插件,支持 HarmonyOS/OpenHarmony。调起地图首页、地点搜索、地点详情、路线规划、导航、打车等能力。

Repository
View/report issues

Documentation

Documentation
API reference

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on petalmaps_ohos

Packages that implement petalmaps_ohos