GMAP.NET - 通过单击地图将标记放置在地图的 X、Y 坐标中
GMAP.NET - Put marker in X,Y coordinates of the map by clicking in the map
我已经阅读了很多关于此的内容 API。我从它开始,最后我使用以下代码通过在地图中使用 MouseDown 事件单击来放置标记:
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;
using System.Globalization;
namespace GMapApplicationCSharp
{
public partial class Form1 : Form
{
//GMarkerGoogle marker;
GMapOverlay markers = new GMapOverlay("markers");
public Form1()
{
InitializeComponent();
}
private void mapaButton_Click(object sender, EventArgs e)
{
mapa.DragButton = MouseButtons.Left;
mapa.CanDragMap = true;
mapa.MapProvider = GMapProviders.GoogleMap;
mapa.SetPositionByKeywords("Madrid, Spain");
mapa.MinZoom = 0;
mapa.MaxZoom = 24;
mapa.Zoom = 9;
mapa.AutoScroll = true;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerAndCache;
//mapa.Overlays.Add(markers);
}
private void recargaMapaButton_Click(object sender, EventArgs e)
{
if (IsMapas.Text == "Google Maps Satélite")
mapa.MapProvider = GMapProviders.GoogleSatelliteMap;
if (IsMapas.Text == "Google Maps Callejero")
mapa.MapProvider = GMapProviders.GoogleMap;
if (IsMapas.Text == "Google Maps Híbrido")
mapa.MapProvider = GMapProviders.GoogleHybridMap;
if (IsMapas.Text == "OpenStreetMap")
mapa.MapProvider = GMapProviders.OpenStreetMap;
if (IsMapas.Text == "OpenCycleMap")
mapa.MapProvider = GMapProviders.OpenCycleMap;
mapa.Refresh();
}
//private void mapa_MouseClick(object sender, MouseEventArgs e)
//{
//double lat;
//double lng;
//if (e.Button == System.Windows.Forms.MouseButtons.Left)
//{
// lat = mapa.FromLocalToLatLng(e.X, e.Y).Lat;
// lng = mapa.FromLocalToLatLng(e.X, e.Y).Lng;
// GMapOverlay markers = new GMapOverlay("markers");
// GMapMarker marker = new GMarkerGoogle(new PointLatLng(lat, lng), GMarkerGoogleType.blue_pushpin);
// markers.Markers.Add(marker);
// mapa.Overlays.Add(markers);
// Console.WriteLine("LAT: ");
// Console.WriteLine(lat);
// Console.WriteLine("LON: ");
// Console.WriteLine(lng);
//}
//}
//private void SetMarcadorButton_Click(object sender, EventArgs e)
//{
//}
private void mapa_MouseDown(object sender, MouseEventArgs e)
{
//double lat;
//double lng;
//lat = Convert.ToDouble(LatitudTextBox.Text);
//lng = Convert.ToDouble(LongitudTextBox.Text);
//GMapOverlay markers = new GMapOverlay("markers");
//GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(lat, lng), GMarkerGoogleType.blue_pushpin);
//markers.Markers.Add(marker);
//mapa.Overlays.Add(markers);
//base.OnMouseMove(e);
//Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
double X = mapa.FromLocalToLatLng(e.X, e.Y).Lng;
double Y = mapa.FromLocalToLatLng(e.X, e.Y).Lat;
//string longitude = X.ToString();
//string latitude = Y.ToString();
//LatitudTextBox.Text = Convert.ToString(double.Parse(latitude, CultureInfo.InvariantCulture));
//LongitudTextBox.Text = Convert.ToString(double.Parse(longitude, CultureInfo.InvariantCulture));
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(Y, X), GMarkerGoogleType.blue_pushpin);
markers.Markers.Add(marker);
mapa.Overlays.Add(markers);
}
}
}
抱歉所有评论行..
我的问题是,在第一次点击地图时,它总是将第一个标记放在地图的中心。不知道为什么,只在第一次点击时出现,因为在后面的点击中,我可以看到我点击的坐标中的标记。
也许有什么我没有看到?为什么第一次点击总是把标记放在中间?
另外,如果有人知道,如果我想离线使用应用程序,有什么方法可以将地图存储在缓存中?也许有一些功能或类似的东西。因为我一直在看"ServerOnly"、"ServerAndCache"和"CacheOnly"函数,但我不知道如何正确使用它们。想象一下,我想在没有 Internet 的地方使用我的应用程序。
编辑:
在这里,我将代码留在 VB.NET 中(比另一个更干净)。我已经转换了代码,因为VB是我通常编程的语言,当然,我也有同样的问题:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports GMap.NET.MapProviders
Imports GMap.NET
Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Imports System.Globalization
Public Class Form1
Dim markers As GMapOverlay = New GMapOverlay("markers")
Private Sub CargarButton_Click(sender As Object, e As EventArgs) Handles CargarButton.Click
mapa.DragButton = Windows.Forms.MouseButtons.Left
mapa.CanDragMap = True
mapa.MapProvider = GMapProviders.GoogleMap
mapa.SetPositionByKeywords("Madrid, Spain")
mapa.MinZoom = 0
mapa.MaxZoom = 24
mapa.Zoom = 9
mapa.AutoScroll = True
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerAndCache
End Sub
Private Sub mapa_MouseDown(sender As Object, e As MouseEventArgs) Handles mapa.MouseDown
Dim X As Double = mapa.FromLocalToLatLng(e.X, e.Y).Lng
Dim Y As Double = mapa.FromLocalToLatLng(e.X, e.Y).Lat
Dim marker As GMarkerGoogle = New GMarkerGoogle(New PointLatLng(Y, X), GMarkerGoogleType.green)
markers.Markers.Add(marker)
mapa.Overlays.Add(markers)
End Sub
End Class
将叠加层添加到地图一次。只有这样才能将标记添加到叠加层。这是错误的顺序,因为叠加层未附加到地图实例,您将第一个标记添加到叠加层,因此位置错误。
认为您应该排除缓存部分并创建一个单独的问题。
我已经阅读了很多关于此的内容 API。我从它开始,最后我使用以下代码通过在地图中使用 MouseDown 事件单击来放置标记:
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;
using System.Globalization;
namespace GMapApplicationCSharp
{
public partial class Form1 : Form
{
//GMarkerGoogle marker;
GMapOverlay markers = new GMapOverlay("markers");
public Form1()
{
InitializeComponent();
}
private void mapaButton_Click(object sender, EventArgs e)
{
mapa.DragButton = MouseButtons.Left;
mapa.CanDragMap = true;
mapa.MapProvider = GMapProviders.GoogleMap;
mapa.SetPositionByKeywords("Madrid, Spain");
mapa.MinZoom = 0;
mapa.MaxZoom = 24;
mapa.Zoom = 9;
mapa.AutoScroll = true;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerAndCache;
//mapa.Overlays.Add(markers);
}
private void recargaMapaButton_Click(object sender, EventArgs e)
{
if (IsMapas.Text == "Google Maps Satélite")
mapa.MapProvider = GMapProviders.GoogleSatelliteMap;
if (IsMapas.Text == "Google Maps Callejero")
mapa.MapProvider = GMapProviders.GoogleMap;
if (IsMapas.Text == "Google Maps Híbrido")
mapa.MapProvider = GMapProviders.GoogleHybridMap;
if (IsMapas.Text == "OpenStreetMap")
mapa.MapProvider = GMapProviders.OpenStreetMap;
if (IsMapas.Text == "OpenCycleMap")
mapa.MapProvider = GMapProviders.OpenCycleMap;
mapa.Refresh();
}
//private void mapa_MouseClick(object sender, MouseEventArgs e)
//{
//double lat;
//double lng;
//if (e.Button == System.Windows.Forms.MouseButtons.Left)
//{
// lat = mapa.FromLocalToLatLng(e.X, e.Y).Lat;
// lng = mapa.FromLocalToLatLng(e.X, e.Y).Lng;
// GMapOverlay markers = new GMapOverlay("markers");
// GMapMarker marker = new GMarkerGoogle(new PointLatLng(lat, lng), GMarkerGoogleType.blue_pushpin);
// markers.Markers.Add(marker);
// mapa.Overlays.Add(markers);
// Console.WriteLine("LAT: ");
// Console.WriteLine(lat);
// Console.WriteLine("LON: ");
// Console.WriteLine(lng);
//}
//}
//private void SetMarcadorButton_Click(object sender, EventArgs e)
//{
//}
private void mapa_MouseDown(object sender, MouseEventArgs e)
{
//double lat;
//double lng;
//lat = Convert.ToDouble(LatitudTextBox.Text);
//lng = Convert.ToDouble(LongitudTextBox.Text);
//GMapOverlay markers = new GMapOverlay("markers");
//GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(lat, lng), GMarkerGoogleType.blue_pushpin);
//markers.Markers.Add(marker);
//mapa.Overlays.Add(markers);
//base.OnMouseMove(e);
//Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US");
double X = mapa.FromLocalToLatLng(e.X, e.Y).Lng;
double Y = mapa.FromLocalToLatLng(e.X, e.Y).Lat;
//string longitude = X.ToString();
//string latitude = Y.ToString();
//LatitudTextBox.Text = Convert.ToString(double.Parse(latitude, CultureInfo.InvariantCulture));
//LongitudTextBox.Text = Convert.ToString(double.Parse(longitude, CultureInfo.InvariantCulture));
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(Y, X), GMarkerGoogleType.blue_pushpin);
markers.Markers.Add(marker);
mapa.Overlays.Add(markers);
}
}
}
抱歉所有评论行..
我的问题是,在第一次点击地图时,它总是将第一个标记放在地图的中心。不知道为什么,只在第一次点击时出现,因为在后面的点击中,我可以看到我点击的坐标中的标记。
也许有什么我没有看到?为什么第一次点击总是把标记放在中间?
另外,如果有人知道,如果我想离线使用应用程序,有什么方法可以将地图存储在缓存中?也许有一些功能或类似的东西。因为我一直在看"ServerOnly"、"ServerAndCache"和"CacheOnly"函数,但我不知道如何正确使用它们。想象一下,我想在没有 Internet 的地方使用我的应用程序。
编辑: 在这里,我将代码留在 VB.NET 中(比另一个更干净)。我已经转换了代码,因为VB是我通常编程的语言,当然,我也有同样的问题:
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports GMap.NET.MapProviders
Imports GMap.NET
Imports GMap.NET.WindowsForms
Imports GMap.NET.WindowsForms.Markers
Imports System.Globalization
Public Class Form1
Dim markers As GMapOverlay = New GMapOverlay("markers")
Private Sub CargarButton_Click(sender As Object, e As EventArgs) Handles CargarButton.Click
mapa.DragButton = Windows.Forms.MouseButtons.Left
mapa.CanDragMap = True
mapa.MapProvider = GMapProviders.GoogleMap
mapa.SetPositionByKeywords("Madrid, Spain")
mapa.MinZoom = 0
mapa.MaxZoom = 24
mapa.Zoom = 9
mapa.AutoScroll = True
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerAndCache
End Sub
Private Sub mapa_MouseDown(sender As Object, e As MouseEventArgs) Handles mapa.MouseDown
Dim X As Double = mapa.FromLocalToLatLng(e.X, e.Y).Lng
Dim Y As Double = mapa.FromLocalToLatLng(e.X, e.Y).Lat
Dim marker As GMarkerGoogle = New GMarkerGoogle(New PointLatLng(Y, X), GMarkerGoogleType.green)
markers.Markers.Add(marker)
mapa.Overlays.Add(markers)
End Sub
End Class
将叠加层添加到地图一次。只有这样才能将标记添加到叠加层。这是错误的顺序,因为叠加层未附加到地图实例,您将第一个标记添加到叠加层,因此位置错误。
认为您应该排除缓存部分并创建一个单独的问题。