# 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.

![Sample Telerik UI for ASP.NET MVC Map](https://www.telerik.com/aspnet-mvc/documentation/assets/f8b4390ef5abadd7b5c323a5da029d8a/map-getting-started.png)

## 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](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-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:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

```Razor HtmlHelper
@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 the `id` and the `name` 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.

```Razor HtmlHelper
@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("\u00A9 OpenStreetMap contributors");
    })
    .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.

```Razor HtmlHelper
@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("\u00A9 OpenStreetMap contributors");
    })
    .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.

```Razor HtmlHelper
@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("\u00A9 OpenStreetMap contributors");
    })
    .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](https://demos.telerik.com/aspnet-mvc/map/events).

## 5. (Optional) Reference Existing Map Instances

You can reference the Map instances that you have created and build on top of their existing configuration:

1. Use the `id` attribute of the component instance to establish a reference.

   ```JS script
   <script>
       var map = $("#map").data("kendoMap"); // map is a reference to the existing map instance of the helper.
   </script>
   ```
2. Use the [Map client-side API](https://docs.telerik.com/kendo-ui/api/ui/map#methods) to control the behavior of the widget. In this example, you will use the `center` method to access center the view of the Map
   ```JS script
   <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](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/events-and-methods) article.

## Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

* [Sample code with the Map HtmlHelper](https://netcorerepl.telerik.com/mRlmEblS02CGTQVj42)

## Next Steps

* [Export the Map in PDF, SVG or PNG format](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/diagrams-and-maps/map/export)
* [Use Remote Binding for the markers of the Map](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/diagrams-and-maps/map/remote-binding-markers)

## See Also

* [Using the API of the Map for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/map/api)
* [Client-Side API of the Map](https://www.telerik.com/kendo-jquery-ui/documentation/api/dataviz/ui/map)
* [Server-Side API of the Map](https://www.telerik.com/aspnet-mvc/documentation/api/map)
* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
