C# GMap.NET 강좌 : 제 5강 - 경로 그리기
경로(Route)
경로(Route)
란 지도 위에 그려지는 선을 의미합니다.
다각형(Polygon)
은 도형의 형태로 표현되나, 경로(Route)
는 선분의 형태로 표현됩니다.
주로, 길을 표시하거나 거리를 계산하기 위해 사용합니다.
클래스 코드
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
namespace Project
{
class Map
{
public GMapControl App;
public GMapOverlay RouteOverlay = new GMapOverlay("routes");
public Map(GMapControl app)
{
// App Connection
this.App = app;
this.App.MapProvider = GMapProviders.GoogleMap;
// Default Zoom Level
this.App.Zoom = 16;
this.App.MaxZoom = 25;
this.App.MinZoom = 10;
// Default Position
this.App.Position = new PointLatLng(37.497872, 127.0275142);
// Default Route
List<PointLatLng> points = new List<PointLatLng>()
{
new PointLatLng(37.5011466156529, 127.022337913513),
new PointLatLng(37.4936900579307, 127.025878429413),
new PointLatLng(37.4957670734762, 127.032465934753),
new PointLatLng(37.5031042642487, 127.028818130493)
};
// Route Binding
GMapRoute route = new GMapRoute(points, "route");
route.Stroke = new Pen(Color.Red, 2);
RouteOverlay.Routes.Add(route);
this.App.Overlays.Add(RouteOverlay);
// Debug
Console.WriteLine(route.Distance);
}
}
}
세부 코드
public GMapOverlay RouteOverlay = new GMapOverlay("routes");
경로(Route)
를 표현하는 방법도 오버레이(Overlay)
를 사용하여 지도에 표시합니다.
마커(Maker)
또는 다각형(Polygon)
에서 사용되는 클래스와 동일합니다.
List<PointLatLng> points = new List<PointLatLng>()
{
new PointLatLng(37.5011466156529, 127.022337913513),
new PointLatLng(37.4936900579307, 127.025878429413),
new PointLatLng(37.4957670734762, 127.032465934753),
new PointLatLng(37.5031042642487, 127.028818130493)
};
다각형(Polygon)
에서 사용되는 데이터 구조와 동일합니다.
그려지는 방식에 대해서만 차이점이 존재할 뿐, 동일한 데이터 구조를 갖습니다.
GMapRoute route = new GMapRoute(points, "route");
route.Stroke = new Pen(Color.Red, 2);
RouteOverlay.Routes.Add(route);
this.App.Overlays.Add(RouteOverlay);
GMapRoute
클래스로 points
변수를 경로화 시킵니다.
마커(Marker)
또는 다각형(Polygon)
방식과 동일하게 오버레이에 경로를 추가합니다.
Console.WriteLine(route.Distance);
위도, 경도로 표현된 선분들의 거리를 계산합니다.
거리의 표현 단위는 Km
입니다.
points
데이터의 위도, 경도를 계산하여 거리를 계산할 수 있지만, Distance
속성으로 거리가 계산된 값을 얻을 수 있습니다.
출력 결과
2.3911
공유하기


댓글 남기기