Skip to content

Instantly share code, notes, and snippets.

@Nidal-Bakir
Created January 17, 2024 09:07
Show Gist options
  • Save Nidal-Bakir/657d3494214e1738977af3f9f7591368 to your computer and use it in GitHub Desktop.
Save Nidal-Bakir/657d3494214e1738977af3f9f7591368 to your computer and use it in GitHub Desktop.
Launch a ios/google native map app with lat,long and label
import 'dart:io';
import 'package:url_launcher/url_launcher.dart' as url_launcher;
import 'logger/logger.dart';
class MapsLauncher {
static Uri createCoordinatesUri({
required double latitude,
required double longitude,
String? label,
}) {
if (Platform.isAndroid) {
var query = '$latitude,$longitude';
if (label != null) query += '($label)';
return Uri(scheme: 'geo', host: '0,0', queryParameters: {'q': query});
}
if (Platform.isIOS) {
final params = {'ll': '$latitude,$longitude'};
if (label != null) params['q'] = label;
return Uri.https('maps.apple.com', '/', params);
}
return Uri.https(
'www.google.com',
'/maps/search/',
{'api': '1', 'query': '$latitude,$longitude'},
);
}
static Future<bool> launchMapWithCoordinate({
required double latitude,
required double longitude,
String? label,
}) async {
final uri = createCoordinatesUri(
latitude: latitude,
longitude: longitude,
label: label,
);
try {
if (await url_launcher.canLaunchUrl(uri)) {
return url_launcher.launchUrl(uri);
}
} catch (e, s) {
Logger.e(
'Error while launching the map using url_launcher',
error: e,
stackTrace: s,
tag: 'MapLauncher',
);
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment