Flutter - Open Location in Maps
If you want to use address instead of lat, long, you can use the following.
Referencing https://developers.google.com/maps/documentation/urls/ios-urlscheme, daddr
Sets the end point for directions searches
final url = 'comgooglemaps://?daddr=${Uri.encodeFull(address)}&directionsmode=driving';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
and don't forget to add to Info.plist
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
</array>
I created a plugin Map Launcher for that.
To find installed maps on a device you can use:
import 'package:map_launcher/map_launcher.dart';
final availableMaps = await MapLauncher.installedMaps;
And then launch by calling showMarker on it
await availableMaps.first.showMarker(
coords: Coords(31.233568, 121.505504),
title: "Shanghai Tower",
description: "Asia's tallest building",
);
Or just check if map available and launch it if so
if (await MapLauncher.isMapAvailable(MapType.google)) {
await MapLauncher.launchMap(
mapType: MapType.google,
coords: coords,
title: title,
description: description,
);
}
This solution will work on both Android and iOS platforms.
import 'dart:io' show Platform;
import 'package:flutter/foundation.dart';
import 'package:url_launcher/url_launcher.dart';
class MapsLauncher {
static String createQueryUrl(String query) {
var uri;
if (kIsWeb) {
uri = Uri.https(
'www.google.com', '/maps/search/', {'api': '1', 'query': query});
} else if (Platform.isAndroid) {
uri = Uri(scheme: 'geo', host: '0,0', queryParameters: {'q': query});
} else if (Platform.isIOS) {
uri = Uri.https('maps.apple.com', '/', {'q': query});
} else {
uri = Uri.https(
'www.google.com', '/maps/search/', {'api': '1', 'query': query});
}
return uri.toString();
}
static String createCoordinatesUrl(double latitude, double longitude,
[String? label]) {
var uri;
if (kIsWeb) {
uri = Uri.https('www.google.com', '/maps/search/',
{'api': '1', 'query': '$latitude,$longitude'});
} else if (Platform.isAndroid) {
var query = '$latitude,$longitude';
if (label != null) query += '($label)';
uri = Uri(scheme: 'geo', host: '0,0', queryParameters: {'q': query});
} else if (Platform.isIOS) {
var params = {'ll': '$latitude,$longitude'};
if (label != null) params['q'] = label;
uri = Uri.https('maps.apple.com', '/', params);
} else {
uri = Uri.https('www.google.com', '/maps/search/',
{'api': '1', 'query': '$latitude,$longitude'});
}
return uri.toString();
}
static Future<bool> launchQuery(String query) {
return launch(createQueryUrl(query));
}
static Future<bool> launchCoordinates(double latitude, double longitude,
[String? label]) {
return launch(createCoordinatesUrl(latitude, longitude, label));
}
}
Call it like this: MapsLauncher.launchCoordinates( 37.4220041, -122.0862462, "address");
Yes you can do that using the url_launcher
plugin. The following code will open Google Maps when the app is installed on the phone (otherwise it will open the browser):
void _launchMapsUrl(double lat, double lon) async {
final url = 'https://www.google.com/maps/search/?api=1&query=$lat,$lon';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}