@@ -548,9 +548,9 @@ private List<BaseLabel> CreateLabelDefinitions(Graphics g, MapViewport map, Feat
548548
549549 if ( feature . Geometry is IGeometryCollection )
550550 {
551+ var geoms = feature . Geometry as IGeometryCollection ;
551552 if ( MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum . All )
552553 {
553- var geoms = feature . Geometry as IGeometryCollection ;
554554 for ( int j = 0 ; j < geoms . Count ; j ++ )
555555 {
556556 BaseLabel lbl = CreateLabelDefinition ( feature , geoms . GetGeometryN ( j ) , text , rotation ,
@@ -561,30 +561,47 @@ private List<BaseLabel> CreateLabelDefinitions(Graphics g, MapViewport map, Feat
561561 }
562562 else if ( MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum . CommonCenter )
563563 {
564- BaseLabel lbl = CreateLabelDefinition ( feature , feature . Geometry , text , rotation , priority , style , map , g , _getLocationMethod , factory ) ;
565- if ( lbl != null )
566- labels . Add ( lbl ) ;
564+ if ( geoms . NumGeometries > 0 )
565+ {
566+ var pt = geoms . Centroid ;
567+ var closest = double . MaxValue ;
568+ var idxOfClosest = 0 ;
569+ for ( var j = 0 ; j < geoms . NumGeometries ; j ++ )
570+ {
571+ var geom = geoms . GetGeometryN ( j ) ;
572+ var dist = geom . Distance ( pt ) ;
573+ if ( dist < closest )
574+ {
575+ closest = dist ;
576+ idxOfClosest = j ;
577+ }
578+ }
579+
580+ BaseLabel lbl = CreateLabelDefinition ( feature , geoms . GetGeometryN ( idxOfClosest ) , text ,
581+ rotation , priority , style , map , g , _getLocationMethod , factory ) ;
582+ if ( lbl != null )
583+ labels . Add ( lbl ) ;
584+ }
567585 }
568586 else if ( MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum . First )
569587 {
570- if ( ( feature . Geometry as IGeometryCollection ) . NumGeometries > 0 )
588+ if ( geoms . NumGeometries > 0 )
571589 {
572- BaseLabel lbl = CreateLabelDefinition ( feature , ( feature . Geometry as IGeometryCollection ) . GetGeometryN ( 0 ) , text ,
590+ BaseLabel lbl = CreateLabelDefinition ( feature , geoms . GetGeometryN ( 0 ) , text ,
573591 rotation , priority , style , map , g , _getLocationMethod , factory ) ;
574592 if ( lbl != null )
575593 labels . Add ( lbl ) ;
576594 }
577595 }
578596 else if ( MultipartGeometryBehaviour == MultipartGeometryBehaviourEnum . Largest )
579597 {
580- var coll = ( feature . Geometry as IGeometryCollection ) ;
581- if ( coll . NumGeometries > 0 )
598+ if ( geoms . NumGeometries > 0 )
582599 {
583600 var largestVal = 0d ;
584601 var idxOfLargest = 0 ;
585- for ( var j = 0 ; j < coll . NumGeometries ; j ++ )
602+ for ( var j = 0 ; j < geoms . NumGeometries ; j ++ )
586603 {
587- var geom = coll . GetGeometryN ( j ) ;
604+ var geom = geoms . GetGeometryN ( j ) ;
588605 if ( geom is ILineString && ( ( ILineString ) geom ) . Length > largestVal )
589606 {
590607 largestVal = ( ( ILineString ) geom ) . Length ;
@@ -607,7 +624,7 @@ private List<BaseLabel> CreateLabelDefinitions(Graphics g, MapViewport map, Feat
607624 }
608625 }
609626
610- BaseLabel lbl = CreateLabelDefinition ( feature , coll . GetGeometryN ( idxOfLargest ) , text , rotation , priority , style ,
627+ BaseLabel lbl = CreateLabelDefinition ( feature , geoms . GetGeometryN ( idxOfLargest ) , text , rotation , priority , style ,
611628 map , g , _getLocationMethod , factory ) ;
612629 if ( lbl != null )
613630 labels . Add ( lbl ) ;
0 commit comments