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
- get an api key at https://cloud.google.com/maps-platform/.
- 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.
- in
android/app/src/main/androidmanifest.xml
insideapplication tag
add your key
<manifest ...
<application ...
<meta-data android:name="com.google.android.geo.api_key"
android:value="your key here"/>
- 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)
}
}
- use the
googlemapswidget.dart
inside thelib/widget
folder as normal widget and use it where you want.
adding custom marker to the map
adding normal marker
- declare a set of markers that will be shown on the map
set<marker> _markers = set<marker>();
- add the set of markers to googlemap widget
googlemap(
markers: _markers,
- 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)]);
}
- 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
- declare a bitmapdescriptor which will hold the customicon
late bitmapdescriptor customicon;
- 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();
}
- 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
- go to https://mapstyle.withgoogle.com/
- choose the old version of the site by choosing no thanks, take me to the old style wizard
- you will find a lot of options, play with it until you get the desired style.
- 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
- 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;
- 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');
}
- 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);
}
- 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
- 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.
- enable google directions, select “directions api” in the “additional apis” section, then select “enable”.
- make sure the apis you enabled are under the “enabled apis” section.
adding route to the map
- declare your start and end points
final latlng initiallatlng = latlng(30.029585, 31.022356);
final latlng destinationlatlng = latlng(30.060567, 30.962413);
- declare polyline and polylinecoordinates
set<polyline> _polyline = {};
list<latlng> polylinecoordinates = [];
- 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();
});
}
- 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"];
- 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));
Comments are closed.