flutter map view
a flutter plugin for displaying google maps on ios and android
please note: api changes are likely as we continue to develop this plugin.
getting started
generate your api key
- go to: https://console.developers.google.com/
- enable
maps sdk for android
- enable
maps sdk for ios
- under
credentials
, choosecreate credential
.- note: for development, you can create an unrestricted api key that can be used on both ios & android.
for production it is highly recommended that you restrict.
- note: for development, you can create an unrestricted api key that can be used on both ios & android.
- more detailed instructions for android can be found here: https://developers.google.com/maps/documentation/android-sdk/signup
- more detailed instructions for ios can be found here: https://developers.google.com/maps/documentation/ios-sdk/get-api-key
the way you register your api key on ios vs android is different. make sure to read the next sections carefully.
ios
the maps plugin will request your users location when needed. ios requires that you explain this usage in the info.plist file
- set the nslocationwheninuseusagedescription in
ios/runner/info.plist
. example:
<key>nslocationwheninuseusagedescription</key>
<string>using location to display on a map</string>
- prior to using the map plugin, you must call mapview.setapikey(string apikey). example:
import 'package:map_view/map_view.dart';
void main() {
mapview.setapikey("<your_api_key>");
runapp(new myapp());
}
note: if your ios and android api key are different, be sure to use your ios api key here.
- add code to show the mapview.
//create an instance variable for the mapview var _mapview = new mapview(); //add a method to call to show the map. void showmap() { _mapview.show(new mapoptions(showuserlocation: true)); }
- run your application on an ios device or simulator.
confirm that when you display the map you see map detail.
if you only see a beige screen it’s possible that your api key is incorrect. when your api
key is incorrect you’ll see messages like this in the console:
clientparametersrequest failed, 7 attempts remaining (0 vs 12). error domain=com.google.httpstatus code=400 "(null)" userinfo={data=<>}
common api key problems for ios
- your bundle id does not match what is registered in the google api console.
when you create an restricted api key in the google api console it asks you to specify your ios bundle id.
make sure that your ios bundle identifier matches the one you registered in the console. - using the wrong key. if you made a separate key for ios and android,
make sure you are using the ios key in the mapview.setapikey() call.
android
you will be making multiple edits to your androidmanifest.xml
file. in your flutter project, you can
find this file location under android/app/src/main
- in your
androidmanifest.xml
, add the following uses-permission above the tag.<uses-permission android:name="android.permission.access_fine_location"/> <uses-permission android:name="android.permission.access_coarse_location"/>
- in your androidmanifest.xml, add the following lines inside of the
application
tag. be sure to replaceyour_api_key
with the one you generated.<meta-data android:name="com.google.android.maps.v2.api_key" android:value="your_api_key"/> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
- add the mapactivity to your androidmanifest.xml
<activity android:name="com.apptreesoftware.mapview.mapactivity" android:theme="@style/theme.appcompat.light.darkactionbar"/>
- in your
android/build.gradle
file. underbuildscript
dependencies
add:classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.2-4'
- run your application on an android device or simulator.
confirm that when you display the map you see map detail.
if you only see a beige screen it’s possible that your api key is incorrect.
static maps for inline display
this plugin does not currently support displaying a google map within the
flutter widget hierarchy. a common workaround for this is to show a static image using the
google static maps api.
included in this plugin is the staticmapprovider class which will allow you to easily generate
a static map. the static maps api also requires an api key and you must enable the api within the google api console.
- go to: https://console.developers.google.com/
- enable
maps static api
- once enabled, you can use the same api key you generated for ios/android.
- initialize the staticmapprovider
var provider = new staticmapprovider('your_api_key');
- the staticmapprovider offers a few different apis for generating static maps. if you
want to generate an image for the current viewport of your full screen interactive map
you can use:
var uri = staticmapprovider.getimageurifrommap(mapview,
width: 900, height: 400);
you can refer to the example project if you run into any issues with these steps.
features
- [x] ios support
- [x] android support
- [x] toolbar support
- [x] update camera position
- [x] add map pins
- [x] receive map pin touch callbacks
- [x] receive map touch callbacks
- [x] receive location change callbacks
- [x] receive camera change callbacks
- [x] zoom to a set of annotations
- [x] customize pin color
- [x] polyline support
- [x] polygon support
- [x] customize pin image
- [x] remove markers, polylines & polygons.
upcoming
- [ ] bounds geometry functions
usage examples
show a map ( with a toolbar )
mapview.show(
new mapoptions(
mapviewtype: mapviewtype.normal,
showuserlocation: true,
initialcameraposition: new cameraposition(
new location(45.5235258, -122.6732493), 14.0),
title: "recently visited"),
toolbaractions: [new toolbaraction("close", 1)]);
get notified when the map is ready
mapview.onmapready.listen((_) {
print("map ready");
});
add multiple pins to the map
mapview.setmarkers(<marker>[
new marker("1", "work", 45.523970, -122.663081, color: colors.blue),
new marker("2", "nossa familia coffee", 45.528788, -122.684633),
]);
add a single pin to the map
mapview.addmarker(new marker("3", "10 barrel", 45.5259467, -122.687747,
color: colors.purple));
edit custom marker image
first add your assets to a folder in your project directory. the name of the folder could be any but “images” or “assets” are the more common.
it should look like this.
- project_name
|-android
|-images
|-flower_vase.png
|-ios
|-lib
# rest of project folders and files
then add asset to the pubspec.yaml under flutter tag.
flutter:
# code already existent
# added asset.
assets:
- images/flower_vase.png
finally use the asset name as icon for your marker. if the width or height is not set or is equals to 0, the image original value of said attribute will be used.
new marker(
"1",
"something fragile!",
45.52480841512737,
-122.66201455146073,
color: colors.blue,
draggable: true, //allows the user to move the marker.
markericon: new markericon(
"images/flower_vase.png", //asset to be used as icon
width: 112.0, //new width for the asset
height: 75.0, // new height for the asset
),
);
set a marker draggable and listening to position changes
first set the draggable attribute of a marker to true.
marker marker=new marker(
"1",
"something fragile!",
45.52480841512737,
-122.66201455146073,
draggable: true, //allows the user to move the marker.
);
now add listeners for the events.
// this listener fires when the marker is long pressed and could be moved.
mapview.onannotationdragstart.listen((markermap) {
var marker = markermap.keys.first;
var location = markermap[marker]; // the original location of the marker before moving it. use it if needed.
print("annotation ${marker.id} dragging started");
});
// this listener fires when the user releases the marker.
mapview.onannotationdragend.listen((markermap) {
var marker = markermap.keys.first;
var location = markermap[marker]; // the actual position of the marker after finishing the dragging.
print("annotation ${marker.id} dragging ended");
});
// this listener fires every time the marker changes position.
mapview.onannotationdrag.listen((markermap) {
var marker = markermap.keys.first;
var location = markermap[marker]; // the updated position of the marker.
print("annotation ${marker.id} moved to ${location.latitude} , ${location
.longitude}");
});
add a single polyline to the map
mapview.addpolyline(new polyline(
"12",
<location>[
new location(45.519698, -122.674932),
new location(45.516687, -122.667014),
],
width: 15.0));
add multiple polylines to the map
mapview.setpolylines(<polyline>[
new polyline(
"11",
<location>[
new location(45.523970, -122.663081),
new location(45.528788, -122.684633),
new location(45.528864, -122.667195),
],
jointtype: figurejointtype.round,
width: 15.0,
color: colors.orangeaccent,
),
new polyline(
"12",
<location>[
new location(45.519698, -122.674932),
new location(45.516687, -122.667014),
],
width: 15.0,
),
]);
add a single polygon to the map
mapview.addpolygon(new polygon(
"111",
<location>[
new location(45.5231233, -122.6733130),
new location(45.5233225, -122.6732969),
new location(45.5232398, -122.6733506),
new location(45.5231233, -122.6733130),
],
jointtype: figurejointtype.round,
strokewidth: 5.0,
strokecolor: colors.red,
fillcolor: color.fromargb(75, 255, 0, 0),
));
add multiple polygons to the map
mapview.setpolygons(<polygon>[
new polygon(
"111",
<location>[
new location(42.9274334, -72.2811234),
new location(42.9258230, -72.2808444),
new location(42.9261294, -72.2779906),
new location(42.9275120, -72.2779155),
],
//you can add a hole inside the polygon
holes: <hole>[
new hole(
<location>[
new location(42.9270721, -72.2797287),
new location(42.9266400, -72.2796750),
new location(42.9267186, -72.2790956),
new location(42.9270014, -72.2790956),
],
),
],
jointtype: figurejointtype.round,
strokewidth: 5.0,
strokecolor: colors.red,
fillcolor: color.fromargb(75, 255, 0, 0)),
new polygon(
"111",
<location>[
new location(45.5231233, -122.6733130),
new location(45.5233225, -122.6732969),
new location(45.5232398, -122.6733506),
new location(45.5231233, -122.6733130),
],
jointtype: figurejointtype.round,
strokewidth: 5.0,
strokecolor: colors.red,
fillcolor: color.fromargb(75, 255, 0, 0)),
]);
remove elements from the map
//remove all markers
mapview.cleara
zoom to fit all the pins on the map
mapview.zoomtofit(padding: 100);
receive location updates of the users current location
mapview.onlocationupdated
.listen((location) => print("location updated $location"));
receive marker, polyline & polygon touches
//marker
mapview.ontouchannotation.listen((annotation) => print("annotation ${annotation.id} tapped"));
//polyline
mapview.ontouchpolyline.listen((polyline) => print("polyline ${polyline.id} tapped"));
//polygon
mapview.ontouchpolygon.listen((polygon) => print("polygon ${polygon.id} tapped"));
receive map touches
mapview.onmaptapped
.listen((location) => print("touched location $location"));
mapview.onmaplongtapped
.listen((location) => print("long tapped location $location"));
receive indoor building & indoor level
mapview.onindoorbuildingactivated.listen(
(indoorbuilding) => print("activated indoor building $indoorbuilding"));
mapview.onindoorlevelactivated.listen(
(indoorlevel) => print("activated indoor level $indoorlevel"));
receive camera change updates
mapview.oncamerachanged.listen((cameraposition) =>
this.setstate(() => this.cameraposition = cameraposition));
receive toolbar actions
mapview.ontoolbaraction.listen((id) {
if (id == 1) {
_handledismiss();
}
});
get the current zoom level
double zoomlevel = await mapview.zoomlevel;
get the maps center location
location centerlocation = await mapview.centerlocation;
get the visible markers on screen
list<marker> visibleannotations = await mapview.visibleannotations;
Comments are closed.