Last active
March 9, 2025 13:31
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// | |
// Copyright (c) 2025 Nidal Bakir | |
// | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
// | |
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