2011-03-31 3 views

답변

1

다각형의 대략적인 중심 만 필요하면 경계 상자의 중심을 찾고 다각형 위에 프로그래밍 방식으로 TextBlock을 추가 할 수 있습니다.

그래서 이런 식으로 뭔가를 작동 할 수 있습니다 :

(XAML)

<MapControl:Map x:Name="MyMap"> 
     <MapControl:Map.Children> 
      <MapControl:MapPolygon Fill="Red" Stroke="Yellow" StrokeThickness="5" Opacity="0.7"> 
       <MapControl:MapPolygon.Locations> 
        <m:LocationCollection> 
         <m:Location>20, -20</m:Location> 
         <m:Location>20, 20</m:Location> 
         <m:Location>-20, 20</m:Location> 
         <m:Location>-20, -20</m:Location> 
        </m:LocationCollection> 
       </MapControl:MapPolygon.Locations> 
      </MapControl:MapPolygon> 
     </MapControl:Map.Children> 
    </MapControl:Map> 

(Codebehind가)

 public partial class MainPage : UserControl 
{ 
    private MapLayer tbLayer; 

    public MainPage() 
    { 
     InitializeComponent(); 

     tbLayer = new MapLayer(); 

     List<TextBlock> newTbs = new List<TextBlock>(); 

     // loop through the maps children and find the polygons 
     foreach (var child in MyMap.Children) 
     { 
      if (child is MapPolygon) 
      { 
       var poly = child as MapPolygon; 

       // get the average lat and long to calculate the "center"-ish of the polygon 
       var avgLat = poly.Locations.Select(l => l.Latitude).Average(); 
       var avgLon = poly.Locations.Select(l => l.Longitude).Average(); 

       TextBlock tb = new TextBlock 
            { 
              Text = "Hey there. I'm a polygon." 
            }; 

       // set the position of the textblock and add it to a new map layer 
       MapLayer.SetPositionOrigin(tb, PositionOrigin.Center); 
       MapLayer.SetPosition(tb, new Location(avgLat, avgLon)); 
       tbLayer.Children.Add(tb); 
      } 
     } 

     // add the new maplayer to the parent map 
     MyMap.Children.Add(tbLayer); 

    } 
} 

당신의 다각형 내 일반적인 예와 같이 작은 사각형이 이상한 모양이 아니라 좋은 경우 , 그러면 좀 더 더러워 질 필요가 있습니다. 이 경우 다각형의 중심을 계산할 수있는 웹 서비스 (WCF)가 필요할 수 있습니다. 나는 Silverlight에서이 작업을 수행하는 간단한 방법이라고 생각하지 않습니다.

  1. 는 WCF 서비스 메서드에 포인트를 보내기 :

    다음과 유사한 과정이 될 것입니다.

  2. 로드까지 아마 그 점을 WKT을 형성하고 SqlGeometry.Parse
  3. 전화를 사용하여 포인트와 SqlGeometry 객체는 당신의 SqlGeometry 객체에 STCentroid.
  4. return SqlGeometry.STAsText 방금 STCentroid를 호출하여 얻은 포인트의 WKT를 반환합니다.

다소 혼란 스럽지만, Silverlight에서 공간적인 작업을하는 것은 항상 내 경험에서 지저분합니다.

도움이되었거나 너무 오래 바람을 피운 희망 :

관련 문제