Reputation: 22995
in my flutter code, I am launching Google Maps app with a predefined location, on a button click. Below is the code
_launchMaps(double lat, double lon) async {
String googleUrl =
'comgooglemaps://?center=${lat},${lon}';
String appleUrl =
'https://www.google.com/maps/search/?api=1&query=$lat,$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';
}
}
In iOS, I did add the following lines to the info.plist file
<key>LSApplicationQueriesSchemes</key>
<array>
<string>googlechromes</string>
<string>comgooglemaps</string>
</array>
In Android
, when I click on the button I get the google maps opened, and I can clearly see the "Directions" button where I can click and start navigating. example below
In iOS I get the Google Map opened, the location is pointed in marker, but I don't get the "Directions" button or anything like that so I can start navigating. how can I fix this?
Upvotes: 2
Views: 8282
Reputation: 1360
Here is a version of the code that works with url_launcher 6.1.12:
String appleUrl = 'https://maps.apple.com/?saddr=&daddr=$lat,$lon&directionsmode=driving';
String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$lat,$lon';
Uri appleUri = Uri.parse(appleUrl);
Uri googleUri = Uri.parse(googleUrl);
if (Platform.isIOS) {
if (await canLaunchUrl(appleUri)) {
await launchUrl(appleUri, mode: LaunchMode.externalApplication);
} else {
if (await canLaunchUrl(googleUri)) {
await launchUrl(googleUri, mode: LaunchMode.externalApplication);
}
}
} else {
if (await canLaunchUrl(googleUri)) {
await launchUrl(googleUri, mode: LaunchMode.externalApplication);
}
}
Upvotes: 0
Reputation: 10443
As mentioned in the docs, you need to define the starting point lat/long and end point lat/long on the URL to prompt for a direction navigation. The URL should look similar to:
comgooglemaps://?saddr={LAT},{LONG}&daddr={LAT},{LONG}&directionsmode=driving
Upvotes: 4
Reputation: 3485
//open google map & apple map app
String appleUrl = 'https://maps.apple.com/?saddr=&daddr=$lat,$lon&directionsmode=driving';
String googleUrl = 'https://www.google.com/maps/search/?api=1&query=$lat,$lon';
if (Platform.isIOS) {
if (await canLaunch(appleUrl)) {
await launch(appleUrl);
} else {
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could not open the map.';
}
}
} else {
if (await canLaunch(googleUrl)) {
await launch(googleUrl);
} else {
throw 'Could not open the map.';
}
}
Also:
// Android
var url = 'geo:$latitude,$longitude';
if (Platform.isIOS) {
// iOS
String query = Uri.encodeComponent(address);
url = 'https://maps.apple.com/?q=$query';
}
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
Upvotes: 1