skip to Main Content

Im using below code to launch map in flutter of specified location but the map showing coordinates instead.

 launchUrl(
Uri.parse('https://www.google.com/maps/search/?api=1&query=${locationLat},${locationLong}'),
mode: LaunchMode.externalApplication);

How to show place name while displaying map.

2

Answers


  1. If you want the map to show the location, you should specify the location name instead of the coordinates in the query argument, start by encoding the name.

    String locationEncoded = Uri.encodeFull(locationName);
    launchUrl(Uri.parse('https://www.google.com/maps/search/api=1&query=$locationEncoded'), mode: LaunchMode.externalApplication);
    

    If you only have the coordinates and do not know the location name, then you can use geocoding https://pub.dev/packages/geocoding to get places infos:

    List<Placemark> placeMarks = [];
    placeMarks = await placemarkFromCoordinates(latitude, longitude);
    
    if (placeMarks.isNotEmpty) {
      location = placeMarks.first.locality?? '';
    }
    

    BONUS

    If the Google map application is installed on the device, you can use intent action for Android https://developers.google.com/maps/documentation/urls/android-intents and IOS https://developers.google.com/maps/documentation/urls/ios-urlscheme so it will open the Google map application instead of the web page.

    The full code will look like:

      var uri = Uri.parse('geo:$latitude,$longitude?q=$locationEncoded');
      if (Platform.isIOS) {
        uri = Uri.parse('comgooglemaps://?center=$latitude,$longitude&q=$locationEncoded');
      }
    
      if (await canLaunchUrl(uri)) {
        await launchUrl(uri);
      }
      else {
        launchUrl(Uri.parse('https://www.google.com/maps/search/?api=1&query=$locationEncoded'));
      }
    

    I hope it will help 🙂

    Login or Signup to reply.
  2. Try this simple code

    String placeName = "Commercial Market";
    String url = "https://www.google.com/maps/place/$placeName";
    
    launchUrl(Uri.parse(url),mode: LaunchMode.externalApplication);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search