title: GMAP.NET - Put marker in X,Y coordinates of the map by clicking in the map
body: <p>I have read a lot about this API. I am starting with it and finally I achieved to put markers by clicking using the MouseDown event in the map using this code:</p>
  
  <pre><code>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);
      }
  }
  }
  </code></pre>
  
  <p>Sorry for all commented lines..</p>
  
  <p>My problem is that in the first click in the map, it always put the first marker in the center of the map. I don't know why, but it only happens in the first click, because in the following clicks, I can see the markers in the coordinates I am clicking.</p>
  
  <p>Maybe there is something I am not seeing? Why the first click always put the marker in the center?</p>
  
  <p>Also, if someone knows, there is some way to store the maps in cache if I want to use the application offline? Maybe with some function or something like that. Because I have been taking a look in the "ServerOnly", "ServerAndCache" and "CacheOnly" functions, but I don't know how to use them properly. Imagine that I want to use my application somewhere where I haven't Internet.</p>
  
  <p><strong>EDIT:</strong>
  Here I leave the code in VB.NET (more cleane that the other one). I have converted the code because VB is the language where I program usually, and, of course, I have the same problem:</p>
  
  <pre><code>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
  </code></pre>