Flutter - APP跳转高德、百度、腾讯、谷歌地图
    		       		warning:
    		            这篇文章距离上次修改已过433天,其中的内容可能已经有所变动。
    		        
        		                
                在Flutter中,要实现APP跳转到高德、百度、腾讯或Google地图,你可以使用url_launcher插件。以下是跳转到高德、百度、腾讯和Google地图的示例代码:
首先,在pubspec.yaml中添加依赖:
dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.0.16然后,使用canLaunch和launch方法来打开对应的地图:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
 
void main() => runApp(MyApp());
 
class MyApp extends StatelessWidget {
  // 高德地图
  String getAmapUrl(double lat, double lon, String name) {
    return 'androidamap://navi?sourceApplication=$name&lat=$lat&lon=$lon&dev=0&style=2';
  }
 
  // 百度地图
  String getBaiduUrl(double lat, double lon, String name) {
    return 'baidumap://map/direction?origin=name:$name|latlng=Current Location|drive|latlng=$lat,$lon';
  }
 
  // 腾讯地图
  String getTencentUrl(double lat, double lon, String name) {
    return 'qqmap://map/routeplan?type=drive&fromname=$name&fromcoord=$lat,$lon&tocoord=$lat,$lon&toname=$name';
  }
 
  // Google地图
  String getGoogleUrl(double lat, double lon, String name) {
    return 'comgooglemaps://?center=$lat,$lon&zoom=14&views=traffic&q=$name';
  }
 
  // 跳转方法
  _launchMapsUrl(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw 'Could not launch $url';
    }
  }
 
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('地图跳转示例'),
        ),
        body: Center(
          child: Column(
            children: <Widget>[
              RaisedButton(
                child: Text('高德地图'),
                onPressed: () => _launchMapsUrl(getAmapUrl(39.990452, 116.481476, '我的位置')),
              ),
              RaisedButton(
                child: Text('百度地图'),
                onPressed: () => _launchMapsUrl(getBaiduUrl(39.990452, 116.481476, '我的位置')),
              ),
              RaisedButton(
                child: Text('腾讯地图'),
                onPressed: () => _launchMapsUrl(getTencentUrl(39.990452, 116.481476, '我的位置')),
              ),
              RaisedButton(
                child: Text('Google地图'),
                onPressed: () => _launchMapsUrl(getGoogleUrl(39.990452, 116.481476, '我的位置')),
              ),
            ],
          ),
        ),
      ),
    );
  }
}请注意,这些URL可能会随着应用版本而变化,并且不同的地图应用可能需要特定的URL格式或参数,所以你可能需要查看相应的地图应用文档以获取最新的URL格式。此外,用户设备上需要安装对应的地图应用。如果用户未安装相应的应用,launch方法将
评论已关闭