I get current location and my destination in app, when on press on ElevatedButton I want open Routing applications like google maps or Waze in user mobile.
ElevatedButton(
onPressed: () {
},
),
how can I do that?
use this package and do it like this:
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.';
}
}
Its better to use this package like this:
Future<void> openMap(double latitude, double longitude) async {
String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
var _params;
Uri googleUri = Uri.parse(googleUrl).replace(queryParameters: _params);
if (await canLaunchUrl(googleUri)) {
await launchUrl(googleUri);
} else {
throw 'Could not open the map.';
}
}
Related
How to Navigate Latitude & Longitude to GoogleMap and MapKit in Flutter without using Dependency.
Am using Dependency this Dependency - url_launcher: ^6.1.4
static Future<void> openMap(double latitude, double longitude) async {
String iosUrl = 'https://maps.apple.com/?q=$latitude,$longitude';
if (GetPlatform.isAndroid) {
String googleUrl ='https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could not launch $googleUrl';
}
}else{
if (await canLaunch(iosUrl)) {
await launch(iosUrl);
} else {
throw 'Could not open the map.';
}
}
}
This above code is Working Fine and Running properly. But I need Solution without Dependency
// custom function to locate the location to direct using google maps app
Future<void> openMap(List<CustAddressModel>latitude,longitude) async {
String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude';
await canLaunchUrlString(googleUrl)
? await launchUrlString(googleUrl)
: throw 'Could not launch google map $googleUrl';
}
#override
void initState() {
// TODO: implement initState
super.initState();
custLocation();
}
}
Here i want to custom function openMap() where user can click button to direct the location using google maps app. Location 1 where the seller location i retrieve from database and the location 2 is customer location i retrieve from database also. Now i want to pass the lat and long from both location 1 and 2, so that user can direct to google maps app to direct to seller location
you can use the packege url_launcher and then add following code:
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:
onTap: () {
final snapshot = await Firestore.instance.collection("collectionName").document('docId').get();
final lat = snapshot.data['latitude'];
final long = snapshot.data['longitude'];
MapUtils.openMap(lat, long);
};
On iOS you need to do some extra steps is that, write following lines in info.plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
i would like to launch custom dynamic links..
i have getter which load string text from database, this string text is user input with them IG username
bio is IG username, exsample #instagram
String getBio(String bio) {
if (widget.isMyProfile) {
return bio;
} else if (bio == "") {
return "";
} else {
return bio;
}
}
i want to have static link "**https://instagram.com/**;" where end of link will be dynamic instagram user name
example: 'https://instagram.com/instagram';
_launchURLIG() async {
const url = 'https://instagram.com/';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
=====
something like this. https://instagram.com' + getBio
_launchURLIG() async {
const url = 'https://instagram.com' + getBio ;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
You should past profile as a parameter to your function
_launchURLIG(String userId) async {
String url = 'https://instagram.com/' + userId ;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
Maybe you need this package to run link:
https://pub.dev/packages/url_launcher
like the title suggests, I use url_launcher to open some websites when the user taps a selected icon. The issue is that it opens the browser website if the associated app is not installed on the device otherwise nothing happens if said app is installed. The app just doesn't respond. From what I've read, its supposed to open the associated app anyway? Or am I mistaken?
Here's the onTap:
GestureDetector(
onTap: _launchTwitchURL,
child: Image.asset(
'assets/images/icon_twitch.png', // On click should redirect to an URL
width: 40.0,
height: 40.0,
),
),
And here's the call:
Future<void> _launchTwitchURL() async {
const url = 'https://www.twitch.tv/example';
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Using android-intent package like #iDecode suggested to get it to work. appavailability might be a better suited/cleaner package depending on your needs.
Future<void> _launchTwitchURL() async {
const url = 'https://www.twitch.tv/example';
if (await canLaunch(url)) {
await launch(url);
} else if (Platform.isAndroid) {
final AndroidIntent intent = AndroidIntent(
action: 'action_view',
data: 'https://www.twitch.tv/example', // replace com.example.app with your applicationId
);
await intent.launch();
} else {
throw 'Could not launch $url';
}
}
Thanks #iDecode
I have a function like this. Using Location package from flutter it shows the dialog to enable GPS.
Future<bool> _checkServiceStatus() async {
final Location location = Location();
bool serviceStatus = await location.serviceEnabled();
if (!serviceStatus) {
serviceStatus = await location.requestService();
print('status -> $serviceStatus');
}
return serviceStatus;
}
When its calling await location.requestService(), it is showing the dialog to enable GPS but after that it never returns the result.
Even its not executing the print() function.
What am i doing wrong here?
Any help would be very appreciated! Thanks in advance.
I had the same issue. It could be solved by upgrading your Flutter project, follow this link https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects
Try this code to check wather permission enabled, service enabled than it returns true else false. Must configure "location" package related configuration in android and ios projects.
Future<bool> checkServiceStatus() async {
final Location location = Location();
final locationPermission = await location.hasPermission();
if (locationPermission == PermissionStatus.granted) {
final locationServiceEnabled = await location.serviceEnabled();
if (locationServiceEnabled == true) {
return true;
} else {
final requestServiceStatus = await location.requestService();
if (requestServiceStatus == true) {
return true;
} else {
BotToast.showSimpleNotification(
title: "Enable GPS to allow this feature");
return false;
}
}
} else {
BotToast.showSimpleNotification(title: "Required location permission to allow this feature");
return false;
}
}