Annotations
Annotations play a crucial role in enhancing the functionality and visual representation of your map-based SDK. They are elements like markers, lines, circles, and polygons that can be placed on the map to indicate specific locations, routes, or areas of interest.
To begin interacting with these annotations and control their behavior, you must first obtain the map controller. This controller acts as the gateway for managing and manipulating annotations on the map.
To operate the mapview, you need to get the map controller in the onMapReady(NextBillionMapController controller)
callback.
By leveraging the onMapReady
callback, you can access the NextBillionMapController instance, which grants you the authority to add, remove, and modify various annotations based on user interactions or programmatic requirements.
Symbol Annotation
The Symbol annotation adds a symbol to the map. It is configured by the specified custom SymbolOptions. If you need to add an image symbol, you need to add the image source to the map style.
Add image source
The addImage() function allows you to include custom images as map annotations (symbols) on the Flutter map. The image data is loaded from the assets directory and identified by a unique key, such as ic_marker
. The addImage() method is asynchronous, ensuring that the image is added before proceeding.
The addSymbol() function enables you to add a new annotation (symbol) to the map at a specified location with custom icon properties. The addSymbol() method requires SymbolOptions as its argument, containing information such as the geometry
(latitude and longitude) and the iconImage
(the previously added image key, e.g., ic_marker
). Additionally, the iconSize
property can be set to scale the icon size.
The removeSymbol() function removes an existing annotation (symbol) from the map. It requires the symbol variable as its argument, representing the specific annotation to be removed. Ensure that the controller is initialized before calling this method to prevent runtime errors.
The updateSymbol() function allows you to modify the properties of an existing annotation (symbol) on the map. The symbol variable must be provided as the first argument, representing the specific annotation to be updated. The updatedOptions
variable contains the new options for the annotation, which could include changes to the geometry, iconImage, iconSize, or any other properties associated with the annotation. Ensure that the controller is initialized before calling this method to avoid runtime errors.
Line Annotation
The Line Annotation lets you draw a line on the map, making it easy to mark paths or routes. You can customize the appearance of the line using the specified LineOptions.
The Line Annotation enables you to create lines on the map by specifying a sequence of geographic coordinates as geometry. These coordinates represent the start and end points of the line. Additionally, you can add intermediate points to create complex paths. The appearance of the line can be customized with the following options:
-
lineColor: Sets the color of the line. You can use standard color names or hexadecimal color codes.
-
lineWidth: Determines the width of the line in dp (device-independent pixels).
To manage the Line Annotation on the map, you have the following options:
Remove Annotation:
If you wish to remove a previously added line from the map, use the removeLine() function. Provide the line variable (representing the line to be removed) as the argument.
Update Annotation:
If you want to modify the appearance or other properties of an existing line, use the updateLine() function. The line variable should be the line you want to update, and updatedOptions
should contain the new options you wish to apply to the line. This could include changes to the geometry, lineColor, lineWidth, or other properties associated with the line. Make sure to initialize the controller before using these functions to avoid any potential issues.
With these methods, you can easily create, manage, and customize lines on the map to suit your application's needs.
Fill Annotation
The Fill Annotation allows you to create filled areas on the map, making it simple to highlight regions or boundaries. You can easily customize the appearance of the fill using the specified FillOptions.
The Fill Annotation enables you to create filled regions on the map by specifying a list of geographic coordinates as geometry. These coordinates represent the boundary points of the filled area. You need to provide the coordinates in a list, and the last point should be the same as the first to close the region.
The appearance of the fill can be customized with the following options:
-
fillColor: Sets the color to fill the area. You can use standard color names or hexadecimal color codes.
-
fillOutlineColor: Determines the color of the outline surrounding the filled area.
Remove Annotation:
If you want to remove a previously added fill from the map, use the removeFill() function. Provide the fill variable (representing the fill to be removed) as the argument.
Update Annotation:
If you wish to modify the appearance or other properties of an existing fill, use the updateFill() function. The fill variable should be the fill you want to update, and updatedOptions
should contain the new options you wish to apply to the fill. This could include changes to the geometry, fillColor, fillOutlineColor, or other properties associated with the fill. Remember to initialize the controller before using these functions to ensure smooth functioning.
With these simple and intuitive methods, you can effortlessly create, manage, and customize filled regions on the map, making your map-based applications more informative and visually appealing.
Circle Annotation
The Circle Annotation lets you display circles on the map, allowing you to highlight specific locations or areas with ease. You can customize the circle's appearance using the provided CircleOptions.
With the Circle Annotation, you can place circles on the map by specifying a single geographic coordinate as geometry. This coordinate represents the center of the circle. You can further adjust the circle's appearance with the following options:
-
circleStrokeColor: Sets the color of the circle's outline. You can use standard color names or hexadecimal color codes to choose the desired color.
-
circleStrokeWidth: Determines the width of the circle's outline in dp (device-independent pixels).
-
circleRadius: Sets the radius of the circle in dp (device-independent pixels), determining its size on the map.
By using these customizable options, you can easily add circles to the map and tailor their appearance to suit your application's requirements.
Annotation onTap Callbacks
To handle tap events on annotations (such as fills, circles, lines, and symbols) in your map, you can easily set up callbacks. These callbacks allow you to respond to user interactions, making your map more interactive and user-friendly. Below is a code snippet demonstrating how to add these callbacks to your map and ensure they are properly removed when disposing of the map.
The code snippet above demonstrates how to set up callbacks to handle tap events on different types of annotations in your map. When the map is created, you add the corresponding callbacks for fill (onFillTapped), circle (onCircleTapped), line (onLineTapped), and symbol (onSymbolTapped) annotations.
Each callback function, such as _onFillTapped
, _onCircleTapped
, _onLineTapped
, and _onSymbolTapped
, will be executed when the user taps on the respective annotation type. Inside these callback functions, you can implement the desired behavior, such as displaying additional information, navigating to new screens, showing popups, or performing specific actions based on the tapped annotation.
To prevent memory leaks, it's essential to remove these callbacks when the map is no longer in use. The dispose() method serves this purpose by removing all the registered callbacks using the remove() function.
By following this approach, you can easily handle tap events on different annotation types and create a more interactive and engaging map experience for your users.