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 = ' 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?

0

12 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 = ' 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); 
4

I found my issue: this needs to be in the plist file. The code in the question above is fine. (The SO answer referenced in the question only mentioned the "comgooglemaps" string.)

<key>LSApplicationQueriesSchemes</key> <array> <string>googlechromes</string> <string>comgooglemaps</string> </array> 

Docs:

4

using url launcher

in yaml file: url_launcher: ^5.0.2 last

then you can use this method to open google maps centered to the provided lat and long

more info to maps intent from docs [here][2] launchMap({String lat = "47.6", String long = "-122.3"}) async{ var mapSchema = 'geo:$lat,$long'; if (await canLaunch(mapSchema)) { await launch(mapSchema); } else { throw 'Could not launch $mapSchema'; } } 
1

Do it this way

Full code is given below

static void navigateTo(double lat, double lng) async { var uri = Uri.parse("google.navigation:q=$lat,$lng&mode=d"); if (await canLaunch(uri.toString())) { await launch(uri.toString()); } else { throw 'Could not launch ${uri.toString()}'; } } 

1) in pubspec.yaml

dependencies: flutter: sdk: flutter ... url_launcher: ^5.7.8 

2) Import wherever you want to use

import 'package:url_launcher/url_launcher.dart'; 

3) final you call

onPressed: () { navigateTo(location.lat, location.lng); }, 
1

If you don't have the actual latlong, you can simply pass an address to Google Maps.

void launchMap(String address) async { String query = Uri.encodeComponent(address); String googleUrl = ""; if (await canLaunch(googleUrl)) { await launch(googleUrl); } } 

Of course, the more information you have in the address, the more accurate the search will be. Exactly the same as looking for something on the actual Google Maps page or app.

By the way, you need to url-encode the address before adding it to the URL, to support special characters like spaces. It's only needed for iOS, but hey, we want to develop for all environments out there.

If you want to navigate with directions you can just create a url with source and destination co-ordinates and other coordinates to add as stops.

Steps:

  1. Install url_launcher plugin

  2. write a code like below.

_launchURL(String url) async { if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } const url =' _launchURL(url); 
0
import 'package:url_launcher/url_launcher.dart'; static void launchMapsUrl( sourceLatitude, sourceLongitude, destinationLatitude, destinationLongitude) async { String mapOptions = [ 'saddr=$sourceLatitude,$sourceLongitude', 'daddr=$destinationLatitude,$destinationLongitude', 'dir_action=navigate' ].join('&'); final url = ' if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } 

Here you can use this function directly and pass the required parameters and also import this package

3
static Future<void> openMap(BuildContext context, double lat, double lng) async { String url = ''; String urlAppleMaps = ''; if (Platform.isAndroid) { url = ' } else { urlAppleMaps = ' url = 'comgooglemaps://?saddr=&daddr=$lat,$lng&directionsmode=driving'; if (await canLaunch(url)) { await launch(url); } else { throw 'Could not launch $url'; } } if (await canLaunch(url)) { await launch(url); } else if (await canLaunch(urlAppleMaps)) { await launch(urlAppleMaps); } else { throw 'Could not launch $url'; }} 
0

As follow-up to Roc Boronat's post, the following code can be used for launching the platform specific map application.

Future<void> launchMapUrl(String address) async { String encodedAddress = Uri.encodeComponent(address); String googleMapUrl = ""; String appleMapUrl = ""; if (Platform.isAndroid) { try { if (await canLaunch(googleMapUrl)) { await launch(googleMapUrl); } } catch (error) { throw("Cannot launch Google map"); } } if (Platform.isIOS) { try { if (await canLaunch(appleMapUrl)) { await launch(appleMapUrl); } } catch (error) { throw("Cannot launch Apple map"); } } 

For more information regarding the query parameters in Apple Maps URL, please visit this link.

with 'url_launcher 6.1.0' + physical address instead of lat & lon,

void _pushMap(String address) async { String query = Uri.encodeComponent(address); String googleUrl = "google.navigation:q=$query"; Uri googleUri = Uri.parse(googleUrl); if (await canLaunchUrl(googleUri)) { await launchUrl(googleUri); } } 

This will send an implicit Intent to open related apps including google maps. Haven't tested on iOS devices.

Try this Function

use plugin: intent: ^1.4.0

static void navigateTo(double lat, double lng) async { var uri = Uri.parse("google.navigation:q=$lat,$lng&mode=c"); android_intent.Intent() ..setAction(android_action.Action.ACTION_VIEW) ..setData(uri) ..setPackage("com.google.android.apps.maps") ..startActivity().catchError((e) => print(e)); } 
1

tldr: I think there's an error in the library and canLaunch sometimes returns false even if the url can be launched.

I was trying to open a google maps link () the same way I do for another link from my app, but for whatever reason canLaunch always returned false. So now I launch in a try catch block to make sure it doesn't crash my app, and it's working.

try { launch(url); } catch (error, stack) { // log error } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like