Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

google_maps_flutter_example

a new flutter application to demonstrate how to implement flutter google maps in a flutter application and perfoem advanced tasks on it.

adding map to the app

  1. get an api key at https://cloud.google.com/maps-platform/.
  2. enable google map sdk for each platform.
    • go to google developers console.
    • choose the project that you want to enable google maps on.
    • select the navigation menu and then select “google maps”.
    • select “apis” under the google maps menu.
    • to enable google maps for android, select “maps sdk for android” in the “additional apis” section, then select “enable”.
    • to enable google maps for ios, select “maps sdk for ios” in the “additional apis” section, then select “enable”.
    • make sure the apis you enabled are under the “enabled apis” section.
  3. in android/app/src/main/androidmanifest.xml inside application tag add your key
<manifest ...
  <application ...
    <meta-data android:name="com.google.android.geo.api_key"
               android:value="your key here"/>
  1. in ios/runner/appdelegate.swift add the following lines
import uikit
import flutter
import googlemaps

@uiapplicationmain
@objc class appdelegate: flutterappdelegate {
  override func application(
    _ application: uiapplication,
    didfinishlaunchingwithoptions launchoptions: [uiapplication.launchoptionskey: any]?
  ) -> bool {
    gmsservices.provideapikey("your key here")
    generatedpluginregistrant.register(with: self)
    return super.application(application, didfinishlaunchingwithoptions: launchoptions)
  }
}
  1. use the googlemapswidget.dart inside the lib/widget folder as normal widget and use it where you want.

adding custom marker to the map

flutter_google_maps_example-1

adding normal marker

  1. declare a set of markers that will be shown on the map
set<marker> _markers = set<marker>();
  1. add the set of markers to googlemap widget
googlemap(
      markers: _markers,
  1. update the set of markers after the map is created in onmapcreated
googlemap(
      onmapcreated: (googlemapcontroller controller) {
              _controller.complete(controller);
              _setmappins([latlng(30.029585, 31.022356)]);
            }
  1. using this function the map will be updated with the given markers on it
_setmappins(list<latlng> markerslocation) {
    _markers.clear();
    setstate(() {
      markerslocation.foreach((markerlocation) {
        _markers.add(marker(
          markerid: markerid(markerlocation.tostring()),
          position: markerlocation,
        ));
      });
    });
  }

customizing the markers

  1. declare a bitmapdescriptor which will hold the customicon
late bitmapdescriptor customicon;
  1. inside initstate() assign the needed png to the customicon
@override
  void initstate() {
    bitmapdescriptor.fromassetimage(imageconfiguration(size: size(50, 50)),
            'assets/images/marker_car.png')
        .then((icon) {
      customicon = icon;
    });
    super.initstate();
  }
  1. finally add the customicon to the marker
marker(
     markerid: markerid(markerlocation.tostring()),
     position: markerlocation,
     icon: customicon,
   )

map customization (light/dark mode)

prepare the map styles

  1. go to https://mapstyle.withgoogle.com/
  2. choose the old version of the site by choosing no thanks, take me to the old style wizard
  3. you will find a lot of options, play with it until you get the desired style.
  4. click finish and a pop-up will show with the json code of your style, copy it and add it as a json file in your assets folder
    don’t forgot to mention it in your pubspec.yaml
    you can find two styles in the project’s assets folder

adding styles to the map

  1. declare strings that will hold your style’s json and a bool to control which mode is shown on the map
bool mapdarkmode = true;
late string _darkmapstyle;
late string _lightmapstyle;
  1. in initstate declare the styles
future _loadmapstyles() async {
    _darkmapstyle = await rootbundle.loadstring('assets/map_style/dark.json');
    _lightmapstyle = await rootbundle.loadstring('assets/map_style/light.json');
  }
  1. after creating the map, set the style
onmapcreated: (googlemapcontroller controller) {
          _controller.complete(controller);
          _setmappins([latlng(30.029585, 31.022356)]);
          _setmapstyle();
        },
future _setmapstyle() async {
    final controller = await _controller.future;
    if (mapdarkmode)
      controller.setmapstyle(_darkmapstyle);
    else
      controller.setmapstyle(_lightmapstyle);
  }
  1. to change the style we created a button on the map using the stack widget
positioned(
   top: 100,
   right: 30,
   child: container(
     height: 30,
     width: 30,
     child: iconbutton(
       icon: icon(
         mapdarkmode ? icons.brightness_4 : icons.brightness_5,
         color: theme.of(context).primarycolor,
       ),
       onpressed: () {
         setstate(() {
           mapdarkmode = !mapdarkmode;
           _setmapstyle();
         });
       },
     ),
   )),

drawing routes

activating directions api

  1. go to google developers console.
  2. choose the project that you want to enable google maps on.
  3. select the navigation menu and then select “google maps”.
  4. select “apis” under the google maps menu.
  5. enable google directions, select “directions api” in the “additional apis” section, then select “enable”.
  6. make sure the apis you enabled are under the “enabled apis” section.

adding route to the map

  1. declare your start and end points
final latlng initiallatlng = latlng(30.029585, 31.022356);
final latlng destinationlatlng = latlng(30.060567, 30.962413);
  1. declare polyline and polylinecoordinates
set<polyline> _polyline = {};
list<latlng> polylinecoordinates = [];
  1. after creating the map, set the polyline
onmapcreated: (googlemapcontroller controller) {
          _controller.complete(controller);
          _setmappins([latlng(30.029585, 31.022356)]);
          _setmapstyle();
          _addpolylines();
        },
_addpolylines() {
    setstate(() {
      lat = (initiallatlng.latitude + destinationlatlng.latitude)/2;
      lng= (initiallatlng.longitude + destinationlatlng.longitude)/2;
      _movecamera(13.0);
      _setpolylines();
    });
  }
  1. to set polyline we send a get request to https://www.maps.googleapis.com/maps/api/directions/json with the start location, end location and the api key
final result = await maprepository()
        .getroutecoordinates(initiallatlng, destinationlatlng);
final route = result.data["routes"][0]["overview_polyline"]["points"];
  1. then we translate the results to a polyline using the maputils
_polyline.add(polyline(
    polylineid: polylineid("triproute"),
    //pass any string here
    width: 3,
    geodesic: true,
    points: maputils.converttolatlng(maputils.decodepoly(route)),
    color: theme.of(context).primarycolor));

Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD


Download this source code for
5 USD

Comments are closed.