Learn how to display point, line, and polygon graphics in a map.
You typically use graphics to display geographic data that is not connected to a database and that is not persisted, like highlighting a route between two locations, displaying a search buffer around a selected point, or showing the location of map coordinates entered by the user. Graphics are composed of a geometry, symbol, and attributes.
In this tutorial, you display points, lines, and polygons on a map as graphics.
Prerequisites
Before starting this tutorial:
-
You need an ArcGIS Location Platform or ArcGIS Online account.
-
Your system meets the system requirements.
-
You need an IDE for Flutter - we recommend VS Code.
Develop or download
You have two options for completing this tutorial:
Option 1: Develop the code
To start the tutorial, complete the Display a map tutorial. This creates a map to display the Santa Monica Mountains in California using the topographic basemap from the ArcGIS Basemap Styles service.
Open a Flutter project
-
Open the project you created by completing the Display a map tutorial.
-
Continue with the following instructions to display a point, line, and polygon in the map.
Add a graphics overlay
A graphics overlay is a container for graphics. It is used with a map view to display graphics on a map. You can add more than one graphics overlay to a map view. Graphics overlays are displayed on top of all the other layers.
-
Open lib\main.dart.
-
In
on, create aMap View Ready() GraphicsOverlayand add it to the map view controller.main.dartUse dark colors for code blocks void onMapViewReady() { final map = ArcGISMap.withBasemapStyle(BasemapStyle.arcGISTopographic); _mapViewController.arcGISMap = map; // Create a new graphics overlay object. final graphicsOverlay = GraphicsOverlay(); // Add the graphics overlay to the map view controller. _mapViewController.graphicsOverlays.add(graphicsOverlay); _mapViewController.setViewpoint( Viewpoint.withLatLongScale( latitude: 34.02700, longitude: -118.80500, scale: 72000, ), ); }
Add a point graphic
A point graphic is created using a point and a marker symbol. A point is defined with x and y coordinates, and a spatial reference. For latitude and longitude coordinates, the spatial reference is WGS84.
-
Create an
ArcGISPointand aSimpleMarkerSymbol. To create theArcGISPoint, provide longitude (x) and latitude (y) coordinates, and aSpatialReference. Use theSpatialReference.wgs84convenience property. Create and style aSimpleMarkerSymbol.Point graphics support a number of symbol types such as
SimpleMarkerSymbol,PictureMarkerSymbolandTextSymbol. Learn more about theSymbolclass in the API documentation.main.dartUse dark colors for code blocks _mapViewController.setViewpoint( Viewpoint.withLatLongScale( latitude: 34.02700, longitude: -118.80500, scale: 72000, ), ); // Create an ArcGIS point with long and lat // and set the spatial reference. final point = ArcGISPoint( x: -118.80657463861, y: 34.0005930608889, spatialReference: SpatialReference.wgs84, ); // Create a new orange circle with a solid blue outline symbol. final pointSymbol = SimpleMarkerSymbol( style: SimpleMarkerSymbolStyle.circle, color: Colors.orange, size: 10, ) ..outline = SimpleLineSymbol( style: SimpleLineSymbolStyle.solid, color: Colors.blue, width: 2, ); -
Create a
Graphicwith thepointandpoint. Display theSymbol Graphicby adding it to thegraphic's list of graphics.Overlay main.dartUse dark colors for code blocks // Create an ArcGIS point with long and lat // and set the spatial reference. final point = ArcGISPoint( x: -118.80657463861, y: 34.0005930608889, spatialReference: SpatialReference.wgs84, ); // Create a new orange circle with a solid blue outline symbol. final pointSymbol = SimpleMarkerSymbol( style: SimpleMarkerSymbolStyle.circle, color: Colors.orange, size: 10, ) ..outline = SimpleLineSymbol( style: SimpleLineSymbolStyle.solid, color: Colors.blue, width: 2, ); // Create a graphic using the point and point symbol defined earlier. final pointGraphic = Graphic(geometry: point, symbol: pointSymbol); // Add the graphic to the graphics overlay to display it. graphicsOverlay.graphics.add(pointGraphic);
Run the app
-
Make sure you have an Android emulator, iOS simulator or physical device configured and running.
-
In VS Code, select Run > Run Without Debugging.
You should see a point graphic in Point Dume State Beach.
Add a line graphic
A line graphic is created using a polyline and a line symbol. A polyline is defined as a sequence of points.
Polylines have one or more distinct parts. Each part is a sequence of points. For a continuous line, you can use the Polyline constructor to create a polyline with just one part. To create a polyline with more than one part, use a Polyline.
-
Create a
Polylineand aSimpleLineSymbol. To create thePolyline, call theaddmethod onPoint X Y() PolylineBuilderfor each (x, y) coordinate pair that defines a point along the polyline's path. Create and style aSimpleLineSymbol.Line graphics support a number of symbol types such as
SimpleLineSymbolandTextSymbol. Learn more about theSymbolclass in the API documentation.main.dartUse dark colors for code blocks // Add the graphic to the graphics overlay to display it. graphicsOverlay.graphics.add(pointGraphic); // Create a polyline builder and set its spatial reference. // Use the `addPointXY()` method to add (x,y) coordinates one at a time // that define the polyline's geometry. final polylineBuilder = PolylineBuilder(spatialReference: SpatialReference.wgs84) ..addPointXY(x: -118.82152, y: 34.01395) ..addPointXY(x: -118.81489, y: 34.00806) ..addPointXY(x: -118.80887, y: 34.00166); // Retrieve the polyline's geometry that the builder constructed. final polyline = polylineBuilder.toGeometry(); // Create a solid blue line simple line symbol. final polylineSymbol = SimpleLineSymbol( style: SimpleLineSymbolStyle.solid, color: Colors.blue, width: 3, ); -
Create a
Graphicwith thepolylineandpolyline. Display theSymbol Graphicby adding it to the list of graphics.main.dartUse dark colors for code blocks // Retrieve the polyline's geometry that the builder constructed. final polyline = polylineBuilder.toGeometry(); // Create a solid blue line simple line symbol. final polylineSymbol = SimpleLineSymbol( style: SimpleLineSymbolStyle.solid, color: Colors.blue, width: 3, ); // Create a graphic using the polyline and polyline symbol defined earlier. final polylineGraphic = Graphic(geometry: polyline, symbol: polylineSymbol); // Add the graphic to the graphics overlay to display it. graphicsOverlay.graphics.add(polylineGraphic); -
Use Flutter's hot restart to load your code changes and restart the app.
You should see a point and line graphic along Westward Beach.
Add a polygon graphic
A polygon graphic is created using a polygon and a fill symbol. A polygon is defined as a sequence of points that describe a closed boundary.
Polygons have one or more distinct parts. Each part is a sequence of points describing a closed boundary. For a single area with no holes, you can use the Polygon constructor to create a polygon with just one part. To create a polygon with more than one part, use a PolygonBuilder.
-
Create a
Polygonand aSimpleFillSymbol. To create thePolygon, call theaddmethod onPoint X Y() PolygonBuilderfor each (x,y) coordinate pair that defines a point on the closed boundary. Create and style aSimpleFillSymbol.Polygon graphics support a number of symbol types such as
SimpleFillSymbol,PictureFillSymbol,SimpleMarkerSymbolandTextSymbol. Learn more about theSymbolclass in the API documentation.main.dartUse dark colors for code blocks // Add the graphic to the graphics overlay to display it. graphicsOverlay.graphics.add(polylineGraphic); // Create a polygon builder and set its spatial reference. // Use the `addPointXY()` method to add (x,y) coordinates one at a time // that define the polygon's geometry. final polygonBuilder = PolygonBuilder(spatialReference: SpatialReference.wgs84) ..addPointXY(x: -118.81898, y: 34.01375) ..addPointXY(x: -118.80679, y: 34.02158) ..addPointXY(x: -118.79143, y: 34.01638) ..addPointXY(x: -118.79596, y: 34.00856) ..addPointXY(x: -118.80855, y: 34.00350); // Retrieve the polygon's geometry that the builder constructed. final polygon = polygonBuilder.toGeometry(); // Create an orange fill with solid blue line outline simple fill symbol. final polygonSymbol = SimpleFillSymbol( style: SimpleFillSymbolStyle.solid, color: Colors.orange, outline: SimpleLineSymbol( style: SimpleLineSymbolStyle.solid, color: Colors.blue, width: 2, ), ); -
Create a
Graphicwith thepolygonandpolygon. Display theSymbol Graphicby adding it to the list of graphics.main.dartUse dark colors for code blocks // Retrieve the polygon's geometry that the builder constructed. final polygon = polygonBuilder.toGeometry(); // Create an orange fill with solid blue line outline simple fill symbol. final polygonSymbol = SimpleFillSymbol( style: SimpleFillSymbolStyle.solid, color: Colors.orange, outline: SimpleLineSymbol( style: SimpleLineSymbolStyle.solid, color: Colors.blue, width: 2, ), ); // Create a graphic using the polygon and polygon symbol defined earlier. final polygonGraphic = Graphic(geometry: polygon, symbol: polygonSymbol); // Add the graphic to the graphics overlay to display it. graphicsOverlay.graphics.add(polygonGraphic); -
Hot restart your app.
You should see a point, line, and polygon graphic around Mahou Riviera in the Santa Monica Mountains.
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solution
-
Click the
Download solutionlink underSolutionand unzip the file to a location on your machine. -
Open the project in VS code.
Since the downloaded solution does not contain authentication code, you must first set up authentication to create credentials, then add authentication code and set the developer credentials to the solution.
Set up authentication
To access the secure ArcGIS location services used in this tutorial, you must implement API key authentication or user authentication using an ArcGIS Location Platform or an ArcGIS Online account.
You can implement API key authentication or user authentication in this tutorial. Compare the differences below:
API key authentication
- Users are not required to sign in.
- Requires creating an API key credential with the correct privileges.
- API keys are long-lived access tokens.
- Service usage is billed to the API key owner/developer.
- Simplest authentication method to implement.
- Recommended approach for new ArcGIS developers.
Learn more in API key authentication.
User authentication
- Users are required to sign in with an ArcGIS account.
- User accounts must have privilege to access the ArcGIS services used in application.
- Requires creating OAuth credentials.
- Application uses a redirect URL and client ID.
- Service usage is billed to the organization of the user signed into the application.
Learn more in User authentication.
To complete this tutorial, click on the tab in the switcher below for your authentication type of choice, either API key authentication or User authentication.
Create a new API key access token with privileges to access the secure resources used in this tutorial.
-
Complete the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
-
Copy and paste the API key access token into a safe location. It will be used in a later step.
Add authentication code and set developer credentials
To allow your app users to access ArcGIS location services, you will add authentication code and use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.
-
Open lib/main.dart.
-
In the
main()function, set theArcGISEnvironment.apiKeyvalue to your access token.main.dartUse dark colors for code blocks void main () { ArcGISEnvironment.apiKey = 'YOUR_ACCESS_TOKEN'; runApp( const MaterialApp(home: MainApp()) ); }
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Run the application
Follow these steps to run the application.
-
In VS Code's terminal, run:
Use dark colors for code blocks Copy flutter pub upgrade -
Run:
Use dark colors for code blocks Copy dart run arcgis_maps install -
Make sure you have an Android emulator, iOS simulator or physical device configured and running.
-
In VS Code, select Run > Run Without Debugging.
You should see a point, line, and polygon graphic around Mahou Riviera in the Santa Monica Mountains.