我已经阅读了很多关于这个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"功能,但我不知道如何正确使用它们。想象一下,我想在我无法上网的地方使用我的应用程序。
修改 在这里,我将代码保留在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