title | description | author | ms.author | ms.date | ms.topic | ms.service | services | manager | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Add a shape with Azure Maps | Microsoft Docs |
How to add a shape to a Javascript map |
jingjing-z |
jinzh |
10/30/2018 |
conceptual |
azure-maps |
azure-maps |
codepen |
This article shows you how to add a Shape to the map and update properties of an existing shape on the map.
<iframe height='500' scrolling='no' title='Add a line to a map' src='//codepen.io/azuremaps/embed/qomaKv/?height=500&theme-id=0&default-tab=js,result&embed-version=2&editable=true' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen Add a line to a map by Azure Maps (@azuremaps) on CodePen. </iframe>The first block of code in the code above constructs a Map object. You can see create a map for instructions.
In the second block of code, a data source object is created using the DataSource class. A LineString object is created and added to the data source.
A LineLayer renders line objects wrapped in the DataSource. The last block of code creates and adds a line layer to the map. See properties of a line layer at LineLayerOptions. The data source and the line layer are created and added to the map within the event listener function to ensure that the line is displayed after the map loads fully.
The Line layer several styling options. Here is a tool to try them out.
<iframe height='700' scrolling='no' title='Line Layer Options' src='//codepen.io/azuremaps/embed/GwLrgb/?height=700&theme-id=0&default-tab=result' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen Line Layer Options by Azure Maps (@azuremaps) on CodePen. </iframe> <iframe height='500' scrolling='no' title='Add a circle to a map' src='//codepen.io/azuremaps/embed/PRmzJX/?height=500&theme-id=0&default-tab=js,result&embed-version=2&editable=true' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen Add a circle to a map by Azure Maps (@azuremaps) on CodePen. </iframe>
The first block of code in the code above constructs a Map object. You can see create a map for instructions.
In the second block of code, a data source object is created using the DataSource class. A circle is a Feature of Point and has a subType
property set to "circle" and a radius
property value meters. When a Point feature with a subType of circle is added to a data source, it converts it into a circular polygon within the map.
A PolygonLayer renders data wrapped in the DataSource on the map. The last block of code creates and adds a polygon layer to the map. See properties of a polygon layer at PolygonLayerOptions. The data source and the polygon layer are created and added to the map within the event listener function to ensure that the circle is displayed after the map loads fully.
There are two different ways you can add a polygon to the map. Both are explained in the following examples.
<iframe height='500' scrolling='no' title='Add a polygon to a map ' src='//codepen.io/azuremaps/embed/yKbOvZ/?height=500&theme-id=0&default-tab=js,result&embed-version=2&editable=true' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen Add a polygon to a map by Azure Maps (@azuremaps) on CodePen. </iframe>In the code above, the first block of code constructs a Map object. You can see create a map for instructions.
In the second block of code, a data source object is created using the DataSource class. A Polygon is created from an array of coordinates and added to the data source.
A PolygonLayer renders data wrapped in the DataSource on the map. The last block of code creates and adds a polygon layer to the map. See properties of a polygon layer at PolygonLayerOptions. The data source and the polygon layer are created and added to the map within the event listener function to ensure that the polygon is displayed after the map loads fully.
<iframe height='500' scrolling='no' title='Polygon and line layer to add polygon' src='//codepen.io/azuremaps/embed/aRyEPy/?height=500&theme-id=0&default-tab=js,result&embed-version=2&editable=true' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen Polygon and line layer to add polygon by Azure Maps (@azuremaps) on CodePen. </iframe>In the code above, the first block of code constructs a Map object. You can see create a map for instructions.
In the second block of code, a data source object is created using the DataSource class. A Polygon is created from an array of coordinates and added to the data source.
A PolygonLayer renders data wrapped in the DataSource on the map. See properties of a polygon layer at PolygonLayerOptions. A LineLayer is an array of lines. See properties of a line layer at LineLayerOptions. The third block of code creates polygon and line layers.
The last block of code adds the polygon and line layers to the map. The data source and the layers are created and added to the map within the event listener function to ensure that the polygon is displayed after the map loads fully.
The Polygon layer only has a few styling options. Here is a tool to try them out.
<iframe height='700' scrolling='no' title='LXvxpg' src='//codepen.io/azuremaps/embed/LXvxpg/?height=700&theme-id=0&default-tab=result' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen LXvxpg by Azure Maps (@azuremaps) on CodePen. </iframe>
A Shape class wraps a Geometry or Feature and makes it easy to update and maintain them.
new Shape(data: Feature<data.Geometry, any>)
constructs a shape object and initializes it with the specified feature.
<iframe height='500' scrolling='no' title='Update shape properties' src='//codepen.io/azuremaps/embed/ZqMeQY/?height=500&theme-id=0&default-tab=js,result&embed-version=2&editable=true' frameborder='no' allowtransparency='true' allowfullscreen='true' style='width: 100%;'>See the Pen Update shape properties by Azure Maps (@azuremaps) on CodePen. </iframe>
The first block of code above constructs a Map object. You can see create a map for instructions.
A point is a Feature of Point the class. The second block of code initializes the radius value for the HTML slider element and then constructs and wraps a point object in a Shape class object.
The third code block creates a function that takes the value from the HTML range slider element and changes the radius value using the shape class addProperty method.
In the fourth block of code, a data source object is created using the DataSource class. The point is then added to data source.
A PolygonLayer renders data wrapped in the DataSource on the map. The third block of code creates a polygon layer. See properties of a polygon layer at PolygonLayerOptions. The data source, the click event listener, and the polygon layer are created and added to the map within the event listener function to ensure that the point is displayed after the map loads fully.
For more code examples to add to your maps, see the following articles:
[!div class="nextstepaction"] Add custom HTML
[!div class="nextstepaction"] Show search results