0

I creating a simple app working with maps on c#, using GMap.NET control for it. So, my question is: I want to make a polygons, polylines and markers in one form using another. My code relating to it is:

First form (MapModule):

    GMapOverlay polygonOverlay = new GMapOverlay("polygons");
    GMapOverlay markersOverlay = new GMapOverlay("markers");
    GMapOverlay polylineOverlay = new GMapOverlay("polylines");

Second form (NewFile):

public MapModule _MapModule;
        public Newfile(MapModule MapModule)
        {
            InitializeComponent();
            _MapModule = MapModule;
        }
private void addpolygon_Click(object sender, EventArgs e)
        {
            GMapPolygon polygon = new GMapPolygon(points, "What you need");
            _MapModule.polylineOverlay.Polygons.Add(polygon);
        }

The output is:

'GoogleMaps.MapModule.polylineOverlay' is inaccessible due to its protection level

I think, it must be very simple, but I am completely new in programming.

Valid XHTML.

Andrew
  • 7,602
  • 2
  • 34
  • 42
Golovach Grach
  • 158
  • 2
  • 2
  • 16
  • Something broken in my browser, I can not use markup. Thanks, tarzanbappa! – Golovach Grach Jun 27 '16 at 07:43
  • That's because `polylineOverlay` is not a `public` property. You should declare it as `public GMapOverlay polylineOverlay` for a quick and dirty fix, but that's not the proper way to handle it (you are bypassing encapsulation). You may want to create a getter for that object, for example. – Andrew Jun 27 '16 at 07:48
  • I am COMPLETELY new, I am trying to program on c# during a week. I don't know what is "getter for an object" :( Can you explain me how can I make it for my example to get me a chance to understand it? I want to understand general principle of that. – Golovach Grach Jun 27 '16 at 07:51
  • I would for the moment check if adding the `public` keyword works. Then I'd suggest you do some research about C#, because Stack Overflow is not intended to give a programming course but answer to specific questions. Anyway, you can find some info about getters and setters here: http://stackoverflow.com/questions/17881091/getter-and-setter-declaration-in-net – Andrew Jun 27 '16 at 07:55
  • Public works, yes! But now I can not understand hot to destruct that created objects :( The stuff is I don't need to study c# now, but make one demo app for now. But I am SO bad that I can not figure out WTF is happening around me when I am using c# – Golovach Grach Jun 27 '16 at 07:57
  • .NET uses a garbage collector, you normally don't need to worry about destroying objects or freeing memory, unless the object uses a huge amount of memory or is an unmanaged resource, like a database connection. More about that here: https://msdn.microsoft.com/en-us/library/0xy59wtx – Andrew Jun 27 '16 at 08:00

1 Answers1

1

The first quick and dirty option is to make that object public:

public GMapOverlay polylineOverlay = new GMapOverlay("polylines");

But that's not a good practice, as this way you are breaking the encapsulation principle and you lose control about what happens with your object. If that variable at some point has garbage, it will be harder for you to know why that occurred.

An alternative is to create a getter, so the reference can be accessed publicly although not modified.

private GMapOverlay polylineOverlay = new GMapOverlay("polylines");
public GMapOverlay PolylineOverlay
{
    get
    {
        return this.polylineOverlay;
    }
}

The containing class can access both, but other classes can access only the public property.

Also, if you don't want this object to be created until it's first used, you could do this instead:

private GMapOverlay polylineOverlay;
public GMapOverlay PolylineOverlay
{
    get
    {
        if (this.polylineOverlay == null)
             this.polylineOverlay = new GMapOverlay("polylines")
        return this.polylineOverlay;
    }
}

Although in this case, you should use the public property inside your class, or instantiate it somewhere else.

Andrew
  • 7,602
  • 2
  • 34
  • 42
  • About destruction. Its making more and more polygon, but I can not destruct it because I don't know how. https://pp.vk.me/c604327/v604327505/14d84/fa5Z3UEDGl0.jpg – Golovach Grach Jun 27 '16 at 08:40
  • I think that's not related to destruction but to removing the points from the polygon collection! My guess is that you should check GMapPolygon's documentation about this. Doesn't something like `_MapModule.PolylineOverlay.Polygons.Clear()` work? – Andrew Jun 27 '16 at 08:50
  • Oh, yes, thank you! And last question. I need to make a lot of markers. How can I make it? The comand for making 1 is GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(53.864418, 27.520451), GMarkerGoogleType.green); How to make a lot of them? – Golovach Grach Jun 27 '16 at 08:54
  • PointLatLng - its a data type, where is coordinates, and GMarkerGoogle structure is GMarkerGoogle (PointLatLng Points, GMarkerGoogleType) – Golovach Grach Jun 27 '16 at 09:01
  • I have never used that framework, but from what I see it should be as simple as `markersOverlay.Markers.Add(marker)` with all the markers you want. Check out this page for more info about this: http://www.independent-software.com/gmap-net-tutorial-maps-markers-and-polygons/ – Andrew Jun 27 '16 at 09:04