Search results

OpenStreet Maps in ASP.NET MVC Maps control

26 Oct 2021 / 2 minutes to read

The OpenStreetMap (OSM) is the online Maps provider built by a community of developers; it is free to use under an open license. It allows to view geographical data in a collaborative way from anywhere on the earth. The OSM Maps provides small tile images based on our requests and combines those images into a single image to display the Maps area in the Maps component.

Adding OpenStreetMap

The OSM Maps can be rendered using by setting the layerType property value as OSM.

razor
osm.cs
Copied to clipboard
@using Syncfusion.EJ2;
@Html.EJS().Maps("maps").Layers(l=> {
    l.LayerType(Syncfusion.EJ2.Maps.ShapeLayerType.OSM).UrlTemplate("http://a.tile.openstreetmap.org/level/tileX/tileY.png").Add();
}).Render()
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Changing the tile server of the OpenStreetMap

The OSM tile server can be changed by setting the tile URL in the UrlTemplate property. For more details about the OSM tile server, refer here.

Zooming and Panning

The OSM Maps layer can be zoomed and panned. Zooming helps to get a closer look at a particular area on a Maps for in-depth analysis. Panning helps to move a Maps around to focus the targeted area.

razor
osmzoom.cs
Copied to clipboard
@using Syncfusion.EJ2;
@Html.EJS().Maps("maps").ZoomSettings(zoom=>zoom.Enable(true)).Layers(l=> {
    l.LayerType(Syncfusion.EJ2.Maps.ShapeLayerType.OSM).Add();
}).Render()
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
    }
}

Adding markers and navigation line

Markers can be added to the layers of OSM Maps by setting the corresponding location’s coordinates of latitude and longitude using MarkerSettings. Navigation lines can be added on top of an OSM Maps layer for highlighting a path among various places by setting the corresponding location’s coordinates of latitude and longitude in the NavigationLineSettings.

razor
osmmarker.cs
Copied to clipboard
@using Syncfusion.EJ2;
@using Syncfusion.EJ2.Maps;
@Html.EJS().Maps("maps").Layers(l => l.LayerType('OSM')
   {
       .NavigationLineSettings(ns => {
        ns.Visible(true).Latitude(new double[] { 34.060620, 40.724546})
        .Longitude(new double[] { -118.330491, -73.850344 }).Color("blue").Angle(90)
        .Width(5).Add();
    }).MarkerSettings(marker =>
       {
           marker.Visible(true).DataSource(ViewBag.markerData).Add();
       }).Add();
   }).Render()
Copied to clipboard
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using EJ2_Core_Application.Models;
using Newtonsoft.Json;

namespace EJ2_Core_Application.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.worldmap = GetWorldMap();
            List<MarkerData> data = new List<MarkerData>
            {
                 new MarkerData {latitude= 37.0000, longitude= -120.0000, city= "California" },
                 new MarkerData {latitude= 40.7127, longitude= -74.0059, city= "New York" },
                 new MarkerData {latitude= 42.0000, longitude= -93.0000, city= "Iowa" }
            };
            ViewBag.markerData = data;
            return View();
        }
        public class MarkerData
        {
            public double latitude { get; set; }
            public double longitude { get; set; }
            public string city { get; set; }
        }
    }
}

Sublayer

Any GeoJSON shape can be rendered as a sublayer on top of the OSM Maps layer for highlighting a particular continent or country in OSM Maps by adding another layer and specifying the Type property of Maps layer to SubLayer.