I'm trying to detect whether Google Maps app is installed on iOS, and if so, launch it, if not, launch Apple Maps. Here is what I have so far, but on my phone with Google Maps installed, it isn't detecting it and launching appropriately.
Any ideas?
import 'package:url_launcher/url_launcher.dart'; _launchMaps() async { String googleUrl = 'comgooglemaps://?center=${trip.origLocationObj.lat},${trip.origLocationObj.lon}'; String appleUrl = 'https://maps.apple.com/?sll=${trip.origLocationObj.lat},${trip.origLocationObj.lon}'; if (await canLaunch("comgooglemaps://")) { print('launching com googleUrl'); await launch(googleUrl); } else if (await canLaunch(appleUrl)) { print('launching apple url'); await launch(appleUrl); } else { throw 'Could not launch url'; } }
I pulled the url scheme from here: How would I be able to open google maps when I press a button in my app?
Maps launcher for FlutterA simple package that uses url_launcher to launch the maps app with the proper scheme on all platforms. On iOS, map links as specified by Apple are launched. On Android, the geo intent is used as documented here. For web and other platforms, the plugin will simply launch Google Maps.
you can install the packege url_launcher and use the code down below:
import 'package:url_launcher/url_launcher.dart'; class MapUtils { MapUtils._(); static Future<void> openMap(double latitude, double longitude) async { String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude'; if (await canLaunch(googleUrl)) { await launch(googleUrl); } else { throw 'Could not open the map.'; } } }
Now you can open google maps in your app just call this method:
MapUtils.openMap(-3.823216,-38.481700);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With