Getting Started with the Map
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC Map and highlights the major steps in the configuration of the component.
You will initialize a Map and learn how to zoom in it. Then, you will see how to attach an event handler to the component.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:
To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can use a project template.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through NuGet.
1. Prepare the CSHTML File
The first step is to add the required directives at the top of the .cshtml
document:
-
To use the Telerik UI for ASP.NET MVC HtmlHelpers:
@using Kendo.Mvc.UI
Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.
@using Kendo.Mvc.UI
<h4>Map with event handler</h4>
<p>
</p>
2. Initialize the Map
Use the Map HtmlHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
attributes of the Map element. - The
Layers()
configuration method determines of the type of the layer. - The
Center()
configuration method sets the center of the map. Coordinates are listed as [Latitude, Longitude]. - The
Markers()
configuration method sets the static markers to display on the map.
@using Kendo.Mvc.UI
<h4>Map with event handler</h4>
<p>
@(Html.Kendo().Map()
.Name("map")
.Center(30.268107, -97.744821)
.Layers(layers =>
{
layers.Add()
.Type(MapLayerType.Tile)
.UrlTemplate("https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png")
.Subdomains("a", "b", "c")
.Attribution("© <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>");
})
.Markers(markers =>
{
markers.Add()
.Location(30.268107, -97.744821)
.Shape(MapMarkersShape.PinTarget)
.Tooltip(tooltip => tooltip.Content("Austin, TX"));
})
)
</p>
3. Use the Zoom Configuration of the Map
The next step is to configure Zoom configuration. Typical web maps use zoom levels from 0 (whole world) to 19 (sub-meter features). You can do that by using the Zoom()
configuration.
@using Kendo.Mvc.UI
<h4>Map with event handler</h4>
<p>
@(Html.Kendo().Map()
.Name("map")
.Zoom(5)
.Center(30.268107, -97.744821)
.Layers(layers =>
{
layers.Add()
.Type(MapLayerType.Tile)
.UrlTemplate("https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png")
.Subdomains("a", "b", "c")
.Attribution("© <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>");
})
.Markers(markers =>
{
markers.Add()
.Location(30.268107, -97.744821)
.Shape(MapMarkersShape.PinTarget)
.Tooltip(tooltip => tooltip.Content("Austin, TX"));
})
)
</p>
4. Handle a Map Event
The Map exposes a ZoomStart()
event that you can handle and assign specific functions to the component. In this tutorial, you will use the ZoomStart()
event to display a message when the user start zooming of in the Map.
@using Kendo.Mvc.UI
<h4>Map with event handler</h4>
<p>
@(Html.Kendo().Map()
.Name("map")
.Zoom(5)
.Center(30.268107, -97.744821)
.Layers(layers =>
{
layers.Add()
.Type(MapLayerType.Tile)
.UrlTemplate("https://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png")
.Subdomains("a", "b", "c")
.Attribution("© <a href='https://osm.org/copyright'>OpenStreetMap contributors</a>");
})
.Markers(markers =>
{
markers.Add()
.Location(30.268107, -97.744821)
.Shape(MapMarkersShape.PinTarget)
.Tooltip(tooltip => tooltip.Content("Austin, TX"));
})
.Events(e => e.ZoomStart("onZoomStart"))
)
</p>
<script>
function onZoomStart(){
console.log("zoom started")
}
</script>
For more examples, refer to the demo on using the events of the Map.
5. (Optional) Reference Existing Map Instances
You can reference the Map instances that you have created and build on top of their existing configuration:
-
Use the
id
attribute of the component instance to establish a reference.<script> var map = $("#map").data("kendoMap"); // map is a reference to the existing map instance of the helper. </script>
- Use the Map client-side API to control the behavior of the widget. In this example, you will use the
center
method to access center the view of the Mapscript <script> var map = $("#map").data("kendoMap"); // map is a reference to the existing map instance of the helper. map.center([32.7758, -96.7966]).zoom(10); // set the Map center </script>
For more information on referencing specific helper instances, see the Methods and Events article.
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: