'How to display the route using GMap in C#?
My main problem is that it doesn't display the line between the 2 markers when I click on the display button.It should draw my route between the first pin and the second pin, but it doesn't show me anything at all. I've been trying for some time to figure out what's wrong. This is the code. I also uploaded images, maybe it will help. Can anyone help me with this? Thanks!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GMap.NET.MapProviders;
using GMap.NET;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
namespace Licenta
{
public partial class Map : Form
{
List<PointLatLng> _points;
public Map()
{
InitializeComponent();
_points = new List<PointLatLng>();
}
private void Map_Load(object sender, EventArgs e)
{
GMapProviders.GoogleMap.ApiKey = "@AIzaSyBreQ8N4txQ74BwKwigtnmkSW8Vhisyles";
gMapControl1.ShowCenter = false;
}
private void btnAdauga_Click(object sender, EventArgs e)
{
_points.Add(new PointLatLng(Convert.ToDouble(txtLat.Text), Convert.ToDouble(txtLong.Text)));
}
private void btnSterge_Click(object sender, EventArgs e)
{
if (gMapControl1.Overlays.Count > 0)
{
gMapControl1.Overlays.RemoveAt(0);
gMapControl1.Refresh();
}
}
private void btnAfiseaza_Click(object sender, EventArgs e)
{
var route = GoogleMapProvider.Instance.GetRoute(_points[0], _points[1], false, false, 14);
var r = new GMapRoute(route.Points, "My Route")
{
Stroke = new Pen(Color.Red, 5)
};
var routes = new GMapOverlay("routes");
routes.Routes.Add(r);
gMapControl1.Overlays.Add(routes);
lblDist.Text = route.Distance + " km";
}
GMapOverlay markers = new GMapOverlay("markers");
private void btnIncarca_Click(object sender, EventArgs e)
{
gMapControl1.DragButton = MouseButtons.Left;
gMapControl1.MapProvider = GMapProviders.GoogleMap;
double lat = Convert.ToDouble(txtLat.Text);
double longt = Convert.ToDouble(txtLong.Text);
gMapControl1.Position = new PointLatLng(lat, longt);
gMapControl1.MinZoom = 2;
gMapControl1.MaxZoom = 100;
gMapControl1.Zoom = 7;
PointLatLng point = new PointLatLng(lat, longt);
GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.red_pushpin);
markers.Markers.Add(marker);
gMapControl1.Overlays.Add(markers);
}
private void gMapControl1_Load(object sender, EventArgs e)
{
gMapControl1.SetPositionByKeywords("Timisoara, Romania");
}
private void button5_Click(object sender, EventArgs e)
{
_points.Clear();
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|