unable to drag and one image blocking the map

Topics: Windows Forms
Jul 26, 2012 at 2:46 AM

hello! i'm quite new to visual studio C# and GMap.NET, and i encounter some problems. There this grey square with a red "+" at the center of the grey square and is obstructing the view of the map. I suspect that is because of the grey square with a red "+" that cause the map not able to be drag, but i'm not too sure either because i'm new with these 2 programs. Below are the codes:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using GMap.NET;             //Required
using GMap.NET.WindowsForms; //Required
using GMap.NET.MapProviders; //Required
using System.Windows.Forms;

namespace GPSMap
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            GMapControl MainMap = new GMapControl();
            {
                MainMap.MapProvider = GMapProviders.YahooMap;
                MainMap.SetCurrentPositionByKeywords("USA");
                MainMap.MinZoom = 1;
                MainMap.MaxZoom = 17;
                MainMap.Zoom = 7;
                MainMap.CanDragMap = true;
                MainMap.Dock = DockStyle.Fill;
            }
            Controls.Add(MainMap);
            ResumeLayout(true);
        }
    }

}

Is there any way to solve the problem? Thanks! :)

Jul 26, 2012 at 7:14 AM

oh i manage to solve the problem. :)

Jul 26, 2012 at 8:06 AM

did you added SuspendLayout ?

Jul 26, 2012 at 9:17 AM

so i just add SuspendLayout() below InitializeComponent(); right? 

and i manage to remove the grey square by including the codes in "private void gMapControl1_Load(object sender, EventArgs e)". i was reading the forum and i saw some people added their codes there so i decided to give it a try.

anyway, thanks so much! :)

Jul 26, 2012 at 9:19 AM

oh yes, just to clarify, the demo that has been referring all around the forum page can be found under the "source code" category right?

Jul 26, 2012 at 11:03 AM

indeed

Jul 27, 2012 at 4:08 AM

decided to just use the same post. I would like to customise my own marker design. I'm using a plane image (planetracker.png file) and try to use it as a marker. Is it possible? The codes that i had done is below:

using System.Drawing;
using System.Drawing.Drawing2D;
using GMap.NET;
using GMap.NET.WindowsForms;

namespace GMap.NET.WindowsForms.Markers
{
    public class GMapMarkerImage : GMapMarker
    {
        
        public System.Drawing.Image Images;

        public GMapMarkerImage(PointLatLng p)
            : base(p)
        {
            Images = Image.FromFile("C:\\Users\\HP\\Documents\\Visual Studio 2010\\Projects\\GPSMap3\\GPSMap3\\Resources\\planetracker.png");
        }

        public override void OnRender(Graphics g)
        {
            Images = Image.FromFile("C:\\Users\\HP\\Documents\\Visual Studio 2010\\Projects\\GPSMap3\\GPSMap3\\Resources\\planetracker.png");
            g.DrawImage(Images, System.Convert.ToInt32(LocalPosition.X - Size.Width / 2), System.Convert.ToInt32(LocalPosition.Y - Size.Height / 2), Size.Width, Size.Height);
        }
    }
}

I'm not sure where i have done wrongly, as when i debug the program, the plane image didn't show up. Could anybody please help me? or maybe give me some hints to solve the problem. or which demo i can look at and try to understand and solve the problem. Quite confused and lost here. :/

Thank you!

Jul 27, 2012 at 2:49 PM

Why on earth do you set a hardcoded path to your image in the GMapMarkerImage class?

Try this:

 

GMapMarkerImage m = new GMapMarkerImage(yourpos);

m.Images = Image.FromFile...

yourOverlay.Markers.Add(m);

Jul 29, 2012 at 11:25 AM

AHAHAHAHA i add it into my resource. I'm totally noob at this. hahaha!

Anyway, i manage to solve the problem. Thanks so much for your help! :)

Aug 3, 2012 at 5:43 AM

I have one more question! i'm trying to display marker with the current location of my GPS. I tried to read the demo and look at some discussion but i still can't display the marker with GPS current location. Here are my codes:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using GMap.NET;             //Required
using GMap.NET.WindowsForms; //Required
using GMap.NET.WindowsForms.Markers;
using GMap.NET.MapProviders;//Required
using System.Threading;

namespace GPSMap3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            SuspendLayout();

            GMapControl MainMap = new GMapControl();                          
            GMapOverlay overlayOne = new GMapOverlay(MainMap, "OverlayOne");

            MainMap.MapProvider = GMapProviders.YahooMap;

                MainMap.SetCurrentPositionByKeywords("Singapore");
                MainMap.MinZoom = 1;
                MainMap.MaxZoom = 24;
                MainMap.Zoom = 13;
                MainMap.CanDragMap = true;
                MainMap.RoutesEnabled = true;
                MainMap.DragButton = MouseButtons.Left;
                MainMap.Dock = DockStyle.Fill;
                MainMap.Manager.Mode = AccessMode.ServerAndCache;
            
                Controls.Add(MainMap);
                ResumeLayout(true);
            
        }

        public void button1_Click(object sender, EventArgs e)  //if button1 (running program button) is clicked
        {
            if (!serialPort1.IsOpen)    //if serial port is not open
                try
                {
                    serialPort1.Open(); //Open Serial Port
                    timer1.Enabled = true;
                }
                catch
                {
                    //A message box will display this message, informing user either a wrong port has been chosen, or have not been plugged in.
                    MessageBox.Show("There was an error. Please make sure that the correct port was selected, and the device, plugged in.");
                }
        }

        public void button2_Click(object sender, EventArgs e)  //if button2 (stop program) is clicked
        {
            if (serialPort1.IsOpen)                // if Serial Port is open
            {
                timer1.Enabled = false;
                serialPort1.Close();               //Close Serial Port
                //Remove data displayed in the textbox
                Latitude.Text = "     ";              
                Longitude.Text = "     ";                 
                Altitude.Text = "    ";                   
            }
        }

        public void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) //When microsoft visual studio receive data
        {
            timer1.Enabled = true;
            Thread.Sleep(100);
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            GMapControl MainMap = new GMapControl();                          //Initialise to use GMap Controls
            GMapOverlay overlayOne = new GMapOverlay(MainMap, "OverlayOne");
          
            string[] c = serialPort1.ReadLine().Split(',');  //Stored data seperately by using array & using the Split() function
            Latitude.Text = c[9] + "\n";                     //display Latitude data in textbox
            Longitude.Text = c[10] + "\n";                  //Display Longitude data in textbox
            Altitude.Text = c[11] + "\n";                   //Display Altitude data in textboxy

            double lat = double.Parse(Latitude.Text, CultureInfo.InvariantCulture); //get Latitude data
            double lng = double.Parse(Longitude.Text, CultureInfo.InvariantCulture); //get Longitude data

            GMapMarker m = new GMapMarkerImage(new PointLatLng(lat, lng));              //customise marker
            overlayOne.Markers.Add(m);
            MainMap.Overlays.Add(overlayOne);

        }

    }
}

i manage to get the data through serialPort1 and display it in the textbox. i read the demo and realise that i can actually use the values in the textbox for the marker position. i tried but i still can't display the marker. Does anybody know how i can display the marker once i receive data from the GPS module? and also update the marker as the GPS module position changed? Thanks! i really need help and i'm trying to understand the demo codes as well. Thanks!

Aug 3, 2012 at 7:12 AM

there is few gps demos already, just use common sense man.. wtf with "GMapControl MainMap = new GMapControl();" in timer thread..