I am developing a Flutter app, in which I need to show navigation to the user for a place. So, how can I open a map application from my Flutter app like we do using external intent in Android?
Or their is any flutter-plugin for it?
For opening an external app from your app in android, you need provide packageName of the app. If the plugin finds the app in the device, it will be be launched. But if the the app is not installed in the device then it leads the user to playstore link of the app.
You can easily do it with the help of external_app_launcher. A Flutter plugin that helps you to open another app from your app. The package asks you for four parameters out of which two are mandatory.
I suggest you to use url_launcher dart package.
In this way you can use all url schemas to open (phone
, sms
, and even maps
as in your case).
In order to open Google Maps either in Android and iOS you could use the general Android Maps URI schema as suggested by Hemanth Raj.
_openMap() async {
const url = 'https://www.google.com/maps/search/?api=1&query=52.32,4.917';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
If you want to give a choice on Android you could use the general geo:
URI schema.
If you want specifically open iOS Maps API you can use Cupertino Maps URI schema.
If you choose to distinguish between Android and iOS (not using Google Maps Api schema for all platform) you have to do it also in your open map call in a way like this:
_openMap() async {
// Android
const url = 'geo:52.32,4.917';
if (await canLaunch(url)) {
await launch(url);
} else {
// iOS
const url = 'http://maps.apple.com/?ll=52.32,4.917';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
}
Or you can check the OS at runtime with dart.io
library Platform
class:
import 'dart:io';
_openMap() async {
// Android
var url = 'geo:52.32,4.917';
if (Platform.isIOS) {
// iOS
url = 'http://maps.apple.com/?ll=52.32,4.917';
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Now that I finished hosekeeping (the real one... not some code refactoring... ^^') I can finish my answer.
As I tell you at the beginning with url_launcher you can use all URI Schemas in order to call, send sms, send e-mail etc.
Here some code to do that:
_sendMail() async {
// Android and iOS
const uri = 'mailto:[email protected]?subject=Greetings&body=Hello%20World';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
_callMe() async {
// Android
const uri = 'tel:+1 222 060 888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
// iOS
const uri = 'tel:001-22-060-888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
}
_textMe() async {
// Android
const uri = 'sms:+39 349 060 888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
// iOS
const uri = 'sms:0039-222-060-888';
if (await canLaunch(uri)) {
await launch(uri);
} else {
throw 'Could not launch $uri';
}
}
}
Even if URI schema should be standards (RFC) sometimes the authority
and path
parts of them could differ between frameworks (Android or iOS).
So here I manage the different OSes with exception but, you could do it better with dart.io
library Platform
class:
import 'dart:io'
and then in code:
if (Platform.isAndroid) {
} else if (Platform.isIOS) {
}
I suggest you always test them in both environments.
You can check the Android and iOS schema documenation here:
If you wanna something similar to startActivity in Android (but that works only for Android platform) you can use the dart package android_intent.
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