Option Explicit Sub CreateCentroid() Dim pDoc As IMxDocument Dim pMap As IMap Set pDoc = Application.Document Set pMap = pDoc.FocusMap Dim pFCursor As IFeatureCursor Dim pSelectionSet As ISelectionSet Dim pFSelection As IFeatureSelection Set pFSelection = pMap.Layer(0) Set pSelectionSet = pFSelection.SelectionSet ''set the pfeaturecursor Set pFCursor = Nothing ''Get a cursor from the selected features pSelectionSet.Search Nothing, False, pFCursor Dim pPoly As IPolygon Dim pArea As IArea Dim pPoint As IPoint Dim lat As Long Dim lon As Long Dim pFeat As IFeature Set pFeat = pFCursor.NextFeature Do Until pFeat Is Nothing Set pPoly = pFeat.ShapeCopy Set pArea = pPoly Set pPoint = pArea.Centroid lat = pPoint.X lon = pPoint.Y Set pFeat = pFCursor.NextFeature Loop MsgBox "The Eastings:" & lat & " The Northings:" & lon End Sub
Also, am I getting eastings and northings or latitude and longitude here?
Thanks again for your generous help.
Dim pGraphics As IGraphicsContainer pGraphics = pMap Dim pMarkerElem As IMarkerElement pMarkerElem = New MarkerElement Dim pElement As IElement pElement = pMarkerElem Dim pFCursor As IFeatureCursor Dim pSelectionSet As ISelectionSet Dim pFSelection As IFeatureSelection pFSelection = pMap.Layer(0) pSelectionSet = pFSelection.SelectionSet ''set the pfeaturecursor pFCursor = Nothing ''Get a cursor from the selected features pSelectionSet.Search(Nothing, False, pFCursor) Dim pPoly As IPolygon Dim pArea As IArea Dim pPoint As IPoint Dim pFeat As IFeature pFeat = pFCursor.NextFeature Do Until pFeat Is Nothing pPoly = pFeat.ShapeCopy pArea = pPoly pPoint = pArea.Centroid pElement = New MarkerElement pElement.Geometry = pPoint pGraphics.AddElement(pElement, 0) pFeat = pFCursor.NextFeature Loop pDoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, Nothing, Nothing) pDoc.ActiveView.Refresh()
Option Explicit Sub CreateCentroid() ''provides access to members that control the MXD Dim pDoc As IMxDocument ''Provides access to members that control the map. Dim pMap As IMap ''Set the MXD to this mxd file Set pDoc = Application.Document ''Set the focus to the current map Set pMap = pDoc.FocusMap ''Provides access to members that hand out enumerated features, ''field collections and allows for the updating, deleting and inserting of features. Dim pFCursor As IFeatureCursor ''Provides access to members that manage a set of selected table rows or features. Dim pSelectionSet As ISelectionSet2 ''Provides access to members that control feature selection. Dim pFSelection As IFeatureSelection ''Set the first layer as the active layer Set pFSelection = pMap.Layer(0) ''Set the selected features Set pSelectionSet = pFSelection.SelectionSet ''set the pfeaturecursor Set pFCursor = Nothing ''Get a cursor from the selected features pSelectionSet.Search Nothing, False, pFCursor ''Provides access to members that identify a polygon and permit controlled access to its inner and outer rings. Dim pPoly As IPolygon4 ''Provides access to members that return properties common to rings and polygons. Dim pArea As IArea ''Provides access to members that define two dimensional points. Dim pPoint As IPoint ''Variable to hold the X coordinate Dim lat As Long ''Variable to hold the Y coordinate Dim lon As Long ''Provides access to members that return and set properties of a feature. Dim pFeat As IFeature Set pFeat = pFCursor.NextFeature Do Until pFeat Is Nothing Set pPoly = pFeat.ShapeCopy Set pArea = pPoly Set pPoint = pArea.Centroid lat = pPoint.X lon = pPoint.Y Set pFeat = pFCursor.NextFeature Loop MsgBox "The Eastings:" & lat & " The Northings:" & lon End Sub