ThinkGeo.com    |     Documentation    |     Premium Support

How to handle mapclicks

i want to diaplay two different points on shapefile and save their coordinated in seperate vairables.


but i have problem, how to handle map clicks for e.g when user click first time draw a point and save its coordiantes in variable and when user click second time on differnt place save then again save its coordiantes in an other variable 


here is my code of mapclick event


private void winformsMap1_MapClick(object sender, MapClickWinformsMapEventArgs e)

        {

            txtXCoordinate.Text = e.WorldX.ToString("N4", CultureInfo.InvariantCulture);

            txtYCoordinate.Text = e.WorldY.ToString("N4", CultureInfo.InvariantCulture);



            double latitude1 = Convert.ToDouble(txtXCoordinate.Text, CultureInfo.InvariantCulture);

            double longitude1 = Convert.ToDouble(txtYCoordinate.Text, CultureInfo.InvariantCulture);



            double lat2 = Convert.ToDouble(txtXCoordinate.Text, CultureInfo.InvariantCulture);

            double long2 = Convert.ToDouble(txtYCoordinate.Text, CultureInfo.InvariantCulture);





            Feature usr1 = new Feature(latitude1, longitude1, "Position1");

            Feature usr2 = new Feature(lat2, long2, "Position2");

            InMemoryFeatureLayer points = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("Points");



            if (!points.InternalFeatures.Contains("Position1"))

            {

                points.InternalFeatures.Add("Position1", usr1);

            }



            if (!points.InternalFeatures.Contains("Position2"))

            {

                points.InternalFeatures.Add("Position2", usr2);

            }



            winformsMap1.Refresh();

        }


 


problem in my code is new point doesnot display and all variables have same lat long



Hi Sarah,


Seems like the problem is that you didn't use "BeginTransacton" and "CommitTransaction", please try the code below:




private void winformsMap1_MapClick(object sender, MapClickWinformsMapEventArgs e)
{
txtXCoordinate.Text = e.WorldX.ToString("N4", CultureInfo.InvariantCulture);
txtYCoordinate.Text = e.WorldY.ToString("N4", CultureInfo.InvariantCulture);

double latitude1 = Convert.ToDouble(txtXCoordinate.Text, CultureInfo.InvariantCulture);
double longitude1 = Convert.ToDouble(txtYCoordinate.Text, CultureInfo.InvariantCulture);

double lat2 = Convert.ToDouble(txtXCoordinate.Text, CultureInfo.InvariantCulture);
double long2 = Convert.ToDouble(txtYCoordinate.Text, CultureInfo.InvariantCulture);


Feature usr1 = new Feature(latitude1, longitude1, "Position1");
Feature usr2 = new Feature(lat2, long2, "Position2");
InMemoryFeatureLayer points = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("Points");
points.FeatureSource.BeginTransaction();
if (!points.InternalFeatures.Contains("Position1"))
{
points.FeatureSource.AddFeature("Position1", usr1);
}

if (!points.InternalFeatures.Contains("Position2"))
{
points.FeatureSource.AddFeature("Position2", usr2);
}
points.FeatureSource.CommitTransaction();


winformsMap1.Refresh();
}




Thanks,


Johnny



it didn't work for me both variables have same lat long as it want to use them for routing here is my code 


        PointSshape user1 = new PointShape(e.WorldX, e.WorldY);

            



             PointShape user2 = new PointShape(e.WorldX, e.WorldY);



           



            Feature p1 = new Feature(user1.GetWellKnownBinary(), "position1");

            Feature p2 = new Feature(user2.GetWellKnownBinary(), "position2");



            InMemoryFeatureLayer points = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("points");

            points.FeatureSource.BeginTransaction();

            if (!points.InternalFeatures.Contains("position1"))

            {

                points.FeatureSource.AddFeature(new PointShape( p1.WellKnownBinary));

                

            }



            if (!points.InternalFeatures.Contains("position2"))

            {

                points.FeatureSource.AddFeature(new PointShape(p2.WellKnownBinary));

            }

            points.FeatureSource.CommitTransaction();



            winformsMap1.Refresh();



Sarah,


After checking your code and I found that two same points were generated in one click event, if you want to get the different points, please try this code:


 
bool isFirstClick = true;
private void winformsMap1_MapClick(object sender, ThinkGeo.MapSuite.DesktopEdition.MapClickWinformsMapEventArgs e)
{
    InMemoryFeatureLayer points = (InMemoryFeatureLayer)winformsMap1.FindFeatureLayer("Points");
    if (points != null)
    {
        PointShape firstPoint;
        PointShape secondPoint;
        if (isFirstClick)
        {
            isFirstClick = false;
            firstPoint = new PointShape(e.WorldX, e.WorldY);
            if (!points.InternalFeatures.Contains("firstPoint"))
            {
                points.InternalFeatures.Add("firstPoint", new Feature(firstPoint));
            }
        }
        else
        {
            secondPoint = new PointShape(e.WorldX, e.WorldY);
            if (!points.InternalFeatures.Contains("secondPoint"))
            {
                points.InternalFeatures.Add("secondPoint", new Feature(secondPoint));
            }
        }
    }
    winformsMap1.Refresh();
}


Thanks,


Edgar