Check Marker's position and set center according to the marker postion [Focus to the marker location]

Topics: SharpMap Project, SharpMap v0.9 / v1.x, SharpMap v2.0, WinForms Controls
Oct 28, 2010 at 7:01 AM

Hi Felix,

I am wondring can you help me please?

I want to check marker's position I mean in which location marker is more. suppose I have marker only in France. what i want to do, on click of one button, identify the marker position and and want to focus around the marker.

like in another word we can say that fitting the map to the marker.

here is the example http://econym.org.uk/gmap/basic14.htm and http://econym.org.uk/gmap/example_map14.htm.

 

Please let me know some code example.

thanks for your support felix,

Thanks.

Coordinator
Oct 28, 2010 at 9:56 AM

Vivek, you need to set SharpMap.Map.Center property to adjust the viewport

Hth FObermaier

Oct 28, 2010 at 10:02 AM

Hi Felix,

I want to check first in which area the number of marker is more, i will take take nearby  lat/long and then I want to set center and want to zoom on those points,.

Please let me know this,

Thanks,

 

Coordinator
Oct 28, 2010 at 10:54 AM
Edited Oct 28, 2010 at 10:56 AM

Vivek,

I don't have any sample code, I would try it like this:

  • Define a BoundingBox that is smaller than the Envelope of the map (e.g. 80%, in case of rotation a smaller value might be necessary) but has the same center as the map.
  • Check whether the Marker is in that box
  • If it is, leave everything as is, else
  • Get IntersectionPoint of the above BoundingBox and the Line from Map.Center to Marker
  • Shift Map.Center by the amount of the Vector Marker->IntersectionPoint.

Hth

FObermaier

 

Oct 28, 2010 at 12:17 PM

Hi FObermaier,

If you provide me some code example then it would be great.

I tired but not working :(

I know you can do it.

Thanks,

Vivek

Oct 28, 2010 at 3:15 PM

Hi felix,

Do you get it working?

Thanks,

Oct 28, 2010 at 4:09 PM

Hi felix,

I tried every solution it's not working.

Can you guide me on this?

Thanks,

Vivek

Oct 28, 2010 at 6:45 PM

 

Hi felix,

I tried every solution it's not working.

Can you guide me on this?

Thanks,

Vivek

Coordinator
Oct 29, 2010 at 6:54 AM

vivek, for intersection testing you can use NetTopologySuite or SharpMap.SqlServerSpatialObjects

The rest is straightforward

cheers FObermaier

Oct 29, 2010 at 7:17 AM
Edited Oct 29, 2010 at 7:22 AM

Hi guys. Nice discussion thanks for sharing ideas...

  Minneapolis printing
Carbonless forms printing
Variable data postcard printing


Nov 1, 2010 at 7:50 PM

Hi FObermaier/Vivek/donaldmartin,

Did anyone got it working.

can you people share me some example of it.

I first got this & I thought I will got some solution, I tried FObermaier but not working.

 

Can anyone please let me know this.

 

Thanks,

Johab,

 

Coordinator
Nov 2, 2010 at 10:10 AM
Edited Nov 2, 2010 at 10:12 AM

here you go:

 

public static void EnsureVisible(SharpMap.Map map, SharpMap.Geometries.Point pt)
{
    const double ensureVisibleRatio = 0.1d;
            
    //Get current map envelope
    var bb = map.Envelope;
    System.Console.WriteLine(string.Format("Map envelope: {0}", bb));
            
    //Set valid envelope
    var evbb = bb.Grow(- ensureVisibleRatio * bb.Width, -ensureVisibleRatio * bb.Height );
    System.Console.WriteLine(string.Format("Valid envelope: {0}", evbb));
            
    //Test if Point is in valid envelope
    if (evbb.Contains(pt)) return;

    //It is not
    System.Console.WriteLine(string.Format("Valid envelope does not contain {0}", pt));

    //LineString from Map.Center -> to Point
    var ls = new SharpMap.Geometries.LineString(
        new SharpMap.Geometries.Point[] {evbb.GetCentroid(), pt});
    System.Console.WriteLine(string.Format("LineString Map.Center -> Point: {0}", ls));

    //Setup Linestring from BoundingBox
    var gf = new GisSharpBlog.NetTopologySuite.Geometries.GeometryFactory();
    var evbbpts = new System.Collections.Generic.List<SharpMap.Geometries.Point>(
        new SharpMap.Geometries.Point[] {evbb.TopLeft, evbb.TopRight, 
            evbb.BottomRight, evbb.BottomLeft, evbb.TopLeft });
    var evbblinearring = new SharpMap.Geometries.LineString(evbbpts);
    System.Console.WriteLine(string.Format("Linestring of valid envelope: {0}", evbblinearring));

    // convert geometries to NTS
    var ntsevbb = (GisSharpBlog.NetTopologySuite.Geometries.LineString)
        SharpMap.Converters.NTS.GeometryConverter.ToNTSGeometry(evbblinearring, gf);
    var ntsls = (GisSharpBlog.NetTopologySuite.Geometries.LineString)
        SharpMap.Converters.NTS.GeometryConverter.ToNTSGeometry(ls, gf);

    // Get intersection point
    var intGeo = ntsevbb.Intersection(ntsls);
    var intPt = (GisSharpBlog.NetTopologySuite.Geometries.Point)intGeo;
    System.Console.WriteLine(string.Format("Intersection point is: {0}", intPt));

    //Compute offset
    var dx = pt.X - intPt.X;
    var dy = pt.Y - intPt.Y;
    System.Console.WriteLine(string.Format("Map.Center needs to be shifted by: [{0}, {1}]", dx, dy));

    //Set new center Center
    map.Center = new SharpMap.Geometries.Point(map.Center.X + dx, map.Center.Y + dy);

}

You need to reference SharpMap.Extensions project for this to work. (Is this all that difficult?)

Hth FObermaier

Nov 15, 2010 at 8:40 PM

Hiya,

If I am not wrong, the Answer given by FObermaier will focus around a particular marker and the EnsureVisible function takes the input param as the marker point around which the focus will happen.

But I assume the question asked is in a map focus the area where the marker density is more i.e. focus the part of the map where maximum number of markers seen. 

My suggestion would be as below:

  • Break the map in different small Bounding Box with different center, make sure all these bounding box cover the map in total 
  • count the number of markers for all these bounding boxes respectively
  • Set the map center as any marker position in that bounding box for which you get the maximum count of markers.

Although I am not sure if using sharpmap this can be achieved...

Cheers,
Simon

Nov 17, 2010 at 8:37 AM

Hi Hth FObermaier,

I tried your exmaple code, and it's not working for map focus the area where the marker density is more i.e. focus the part of the map where maximum number of markers seen.  

what we need to do for this?

Please let us know any example of it.

Thanks,

johab

Coordinator
Nov 17, 2010 at 8:59 AM
Edited Nov 17, 2010 at 9:26 AM

johab,

you can get the extent of your layer by calling Layer's Envelope property.

You can then call

myMap.ZoomToBox(myLayer.Envelope);

The "EnsureVisible" function is for moving the viewport of the map without changing the zoom level/factor to a place so that the requested marker is visible.

Hth FObermaier