Open Google Maps from Flutter with place selected - flutter

I am trying to open Google Maps with a place selected. Can the below url be used with dynamic data? The one that I am using selects the first place I searched for on Google Maps. What else do I need to change or is this the right way to open Google Maps from Flutter?
I am using Url Launcher:
import 'package:url_launcher/url_launcher.dart';
Code:
name = Uri.encodeComponent(name);
name = name.replaceAll('%20', '+');
String googleUrl =
'https://www.google.com/maps/place/$name/#$latitude,$longitude,3z/data=!4m8!1m2!2m1!1s$name!3m4!1s0x89c259ae9485299b:0x8b3fcf671f63ac6b!8m2!3d$latitude!4d$longitude';
print(googleUrl);
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could not open the map.';
}
I noticed that each place has a place_id. Can that be used to target specific place?
--- Solution thanks to comment below ---
https://developers.google.com/maps/documentation/urls/get-started

I suggest reading this doc for Google Maps. You can define the center area with a lat/long and the place's name on the URL. This should center the place on the configured coordinates. The URL that you can use should be similar to:
comgooglemaps://?q=$name&center=$lat,$long

Related

How to launch system File Manager from Flutter app?

From my flutter I want to launch the default File Manager. I don't want to select file from it for my app. I just want to launch it (preferably showing the directory that I want it to show) so that the user can pick a file from that directory and do whatever they want with it.
Can anyone please tell me how to do it? I see a lot of code and examples for picking a file but that's not what I need. Just want to launch whatever is a default file manager.
Please do not suggest FilePicker. That is not what I am looking for. I do not want to "pick" a file. I know how to pick a file. I just want the default file manager app to be launched. I don't want the control to be returned back to the app irrespective of whether the user selects a file or not.
This was causing me grief too. Here's my solution.
Using 'external_app_launcher' you are able to launch apps based on their URL scheme, so we have...
import 'package:external_app_launcher/external_app_launcher.dart';
okButton = TextButton(
child: const Text("Open Files"),
onPressed: () async {
await LaunchApp.openApp(iosUrlScheme: 'shareddocuments://', openStore: false);
},
);
This will launch the app, I'm still working my solution as I want to open my App's folder so will update as I get more.
You can use the file_picker package.
Usage
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path);
} else {
// User canceled the picker
}
Edit: Now I understand what you want. Check https://pub.dev/packages/external_app_launcher

How to open the bottom sheet that contains the list of applications for opening URL or google map. So the user can select their preferred application?

I tried this one. But, it Directly opens on the Google Maps application. The bottom sheet for choosing the preferred application is not showing. How do I achieve it? So, the user can select their preferred application.
This is my code for now,
try {
launchUrl(
Uri.parse("geo:${taskAddress.lat},${taskAddress.lng}"),
mode: LaunchMode.externalApplication,
);
} catch (e) {
} finally {
launchUrl(Uri.parse(
"google.navigation:q=${taskAddress.lat},${taskAddress.lng}&mode=d"));
}
I think you can't do that. It's user device preferred setting. If the user set it to "Always Open" or similar it will not ask again.

Adding markers to Mapbox with additional features

I am developing a flutter application in which I am using Mapbox to add turn-by-turn navigation. I have been looking at some tutorials to add markers to the map. However, I also want to give the user awareness of these specific points through voice output.
Is it possible to achieve this objective? Any resources or advice that you could provide, please?
Edit: How can I integrate the text to speech with the custom mapbox on my flutter app? For the Mapbox, we simply run the following code to enable voice instructions.
voiceInstructionsEnabled: true,
So how can I add the text conversion with it?
Truly appreciate any help
You can do it separately.
You can show markers on map with Mapbox
and prepare a string with all marker labels and use flutter_tts package to convert to speech as follows:
Future _speak() async {
await flutterTts.setVolume(volume);
await flutterTts.setSpeechRate(rate);
await flutterTts.setPitch(pitch);
if (_newVoiceText != null) {
if (_newVoiceText!.isNotEmpty) {
await flutterTts.speak(_newVoiceText!);
}
}
}
This snippet is taken from the example given in documentation:
https://pub.dev/packages/flutter_tts/example

Show directions using google map flutter package?

I have an already saved map route as an encoded string in the database. I need to show
already saved route and the direction to the user in the google map using google_maps_flutter package. Even in the google map is okey. There are options to draw the markers and poly lines using the package, but no option to enable direction(like go 300m and then turn left).
here is the code sample I tried, without direction
GoogleMap(
initialCameraPosition: _initialPosition,
myLocationEnabled: true,
trafficEnabled: true,
markers: _markers,
polylines: _polyline,
cameraTargetBounds: _cameraTargetBounds,
)
using google map app we can do that(I'm using map_launcher package to do that like below), but only parsing origin and destination and it's the direction at that time, not the already planned and saved route.
mapLauncher.MapLauncher.showDirections(
mapType: mapLauncher.MapType.google,
origin: mapLauncher.Coords(origin!.latitude, origin.longitude),
originTitle: 'Your Locations',
destinationTitle: 'Destination Location',
destination: mapLauncher.Coords(destination!.latitude, destination.longitude))
does anyone knows a way to achieve this? is it not possible?
There is no FREE way to show directions using Google maps flutter package. You either use another map package or open google maps app (if you're not looking for a FREE way ignore and go all the way down).
FREE WAY
If you would like to open Google Maps app from your app you could do something like this.
First set up your url String (what you will send to Google maps app so it knows what directions give):
String url = 'https://www.google.com/maps/dir/?api=1&origin=' +
currentLocation.latitude.toString() +
',' +
currentLocation.longitude.toString() +
' &destination=' +
lat.toString() +
',' +
lon.toString() +
'&travelmode=driving&dir_action=navigate';
_launchURL(url);
Where currentLocation means the user current location, and lat and lon means the destination.
Then just launch that url, using an urlLauncher:
void _launchURL(String url) async {
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
PAID WAY
If you are super decided to get directions into your app, you can use Directions API.

Is there a way to specify the file name in advance for the picture taken with the flutter camera plugin?

Hi I am using the flutter camera plugin and it works fine, the main issue I am having is that I can't find a way to tell the plugin the image file name when taking the picturee, in android using Kotlin this would be something like:
photoUri = FileProvider.getUriForFile(requireActivity(), "io.awesomedomain", photoFile) // build uri on the app storage space and specific file name -> photoFile.
captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri) // define the pic name before picture is actually taken by the camera.
startActivityForResult(captureImageIntent, REQUEST_PHOTO) // start the camera
I am currently just renaming the picture file name after the picture XFile returns from the flutter camera plugin but this doesn't feel optimal so I am wondering if I am missing something. Is it possible to specify the file name in advance to the flutter camera plugin?
I am currently using the latest available version at the moment: camera: ^0.7.0+2
OK, it looks like I had a misunderstanding of the Camera plugin, I noticed that the returned XFile places the picture in the cache directory and actually provides a method to save the picture to a more definitive storage xfile.saveTo so I did:
var appDir = appDocDirectory.parent.path; // get app directory
file.saveTo('$appDir${Platform.pathSeparator}files${Platform.pathSeparator}${weightPicture.pictureFileName}');
So the picture is properly saved under $myAppDir/files/picName.jpg where the files is a directory I configured to have permission to write to and the picName is defined by the application as I wanted.