Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Google Maps app if available with flutter

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?

like image 603
FrederickCook Avatar asked Nov 01 '17 00:11

FrederickCook


People also ask

How do you launch the map on the Flutter 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.


1 Answers

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); 
like image 93
Clairton Luz Avatar answered Sep 22 '22 03:09

Clairton Luz