I am trying to do something similar to the sample code Identify Point with Icon. The difference between the ThinkGeo code and my code is that my layer is an InMemoryFeatureLayer, the features are PointShapes and I have defined a custom style with several value items determined by the value in a column. Each value item is a GeoImage based on a bitmap.
I have no problem finding the nearest feature. What I am having problems with is getting the feature’s GeoImage so I can determine the dimension of the icon in order to check if the mouse was clicked inside of it. The line in the sample code is:
GeoImage geoImage = shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image;
Since I do not have a default PointStyle, what do I substitute to get the image for the custom style value item that the feature uses?
Regards,
Eileen
Identify Point with Icon
Hi Eileen,
If what I understood is clear, would you please try the below codes:
InMemoryFeatureLayer cityLayer = new InMemoryFeatureLayer();
cityLayer.Open();
cityLayer.Columns.Add(new FeatureSourceColumn(“CNTRY_NAME”));
cityLayer.InternalFeatures.Add(new Feature(new PointShape(-95, 39), new string[] { “CNTRY_NAME:United States” }));
cityLayer.InternalFeatures.Add(new Feature(new PointShape(104.194255828857, 35.8613128662109), new string[] { “CNTRY_NAME:China” }));
PointStyle pointStyle = new PointStyle(new GeoImage(@"…\Data\Image.png"));
PointStyle point2Style = new PointStyle(new GeoImage(@"…\Data\Image2.png"));
ValueStyle valueStyle = new ValueStyle();
valueStyle.ColumnName = “CNTRY_NAME”;
valueStyle.ValueItems.Add(new ValueItem(“United States”, pointStyle));
valueStyle.ValueItems.Add(new ValueItem(“China”, point2Style));
//We can get Image By ‘pointStyle.Image’ and ‘point2Style.Image’.
cityLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(valueStyle);
cityLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
cityLayer.Close();
Any questions, don’t hesitate to let us know.
Thanks,
Johnny
Hi Johnny,
Thanks for your reply, but I think you misunderstood my question. I am able to assign bitmap images to my pointshapes based on the values in the column. It works beautifully. What I want to do now is to be able to click on a point within the image and find the feature. If the mouse click is outside the image, the feature should not be selected. I am following the Think Geo sample code "Identify Point with Icon". I am having problems with one line in particular. Below is the method from the sample code.
//Logic for getting the point feature clicked on based on the dimension of the icon used.
private
void
Map_MouseDown(
object
sender, MouseEventArgs e)
{
//Gets the closest feature from where the user clicked on.
ShapeFileFeatureLayer shapeFileFeatureLayer = (ShapeFileFeatureLayer)mapEngine.StaticLayers[
"PointLayer"
];
shapeFileFeatureLayer.Open();
PointShape clickedPointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, e.X, e.Y, Map.Width, Map.Height);
Collection<Feature> clickedFeatures = shapeFileFeatureLayer.QueryTools.GetFeaturesNearestTo(clickedPointShape, GeographyUnit.DecimalDegree, 1,
ReturningColumnsType.AllColumns);
shapeFileFeatureLayer.Close();
if
(clickedFeatures.Count > 0)
{
//Gets the dimension of the icon and checks if the clicked point is inside it.
GeoImage geoImage = shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image;
ScreenPointF screenPointF = ExtentHelper.ToScreenCoordinate(mapEngine.CurrentExtent, clickedFeatures[0], Map.Width, Map.Height);
RectangleF rectangleF =
new
RectangleF(screenPointF.X - (geoImage.GetWidth() / 2), screenPointF.Y - (geoImage.GetHeight() / 2),
geoImage.GetWidth(), geoImage.GetHeight());
bool
IsInside = rectangleF.Contains(
new
PointF(e.X, e.Y));
//If inside, displays the info of the feature.
if
(IsInside ==
true
)
{
string
text = clickedFeatures[0].ColumnValues[
"NAME"
].Trim();
text = text +
"\r\n"
+
"elev: "
+ clickedFeatures[0].ColumnValues[
"ELEVATION"
].Trim() +
" m"
;
text = text +
"\r\n"
+
"type: "
+ clickedFeatures[0].ColumnValues[
"TYPE"
].Trim();
MessageBox.Show(text,
"Volcano"
, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, (MessageBoxOptions)0);
}
}
}
I am having problems getting the dimensions of the icon so I can check if the user clicked within the image. What do I use for the line
GeoImage geoImage = shapeFileFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.Image;
considering that I do not have DefaultPointStyle.Image but an image that is part of a custom style and based on the value in a particular column?
Regards,
Eileen
Hi Eileen,
Sorry I didn’t explain clear very much in the last reply. Now, I created a sample for you based on it, please check the attached sample to see if it is fit for you?
Regards,
Johnny
Post11695.zip (1.5 MB)
Hi Johnny,
Thanks for creating a sample for me. I was able to apply what you did in your code to my own code and now it works.
Regards,
Eileen
Eileen,
Good to hear it works.
Any questions don’t hesitate to let us know.
Regards,
Johnny