Maps for UWP
手順3:コードファイルの追加
チュートリアル > クリックによるマップマーカーの追加 > 手順3:コードファイルの追加

この手順では、マーカーバルーンの作成や色付けを処理するコードファイルを追加します。

  1. アプリケーション名を右クリックし、リストから[追加]→[新しい項目]を選択します。
  2. 左側ペインで[コード]選択したら、左側ペインでコードファイルを選択します。
  3. 新しいコードファイルに Utils.cs という名前を付け、[OK]をクリックすると、Utils.cs が開きます。
  4. 次の名前空間をインポートします。

    C# コードの書き方

    C#
    コードのコピー
    using C1.Xaml.Maps;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Foundation;
    using Windows.UI;
    using Windows.UI.Xaml.Media;
    using System.Reflection;
    
  5. 名前空間宣言を次のように追加します。
  6. 名前空間宣言の下に、マーカーの作成を処理し、マーカーをランダムな色で表示する次のコードを追加します。

    C# コードの書き方

    C#
    コードのコピー
    namespace YourProjectNameHere
                    {
    
  7. 名前空間宣言の下に、マーカーの作成を処理し、マーカーをランダムな色で表示する次のコードを追加します。

    C# コードの書き方

    C#
    コードのコピー
    public class Utils 
    {     
    public static Geometry CreateBaloon() 
    {        
    PathGeometry pg = new PathGeometry();    
    pg.Transform = new TranslateTransform() { X = -10, Y = -24.14 };   
    PathFigure pf = new PathFigure() { StartPoint = new Point(10, 24.14), 
    IsFilled = true, IsClosed = true };           
    pf.Segments.Add(new ArcSegment() { SweepDirection = SweepDirection.
    Counterclockwise, Point = new Point(5, 19.14), RotationAngle = 45, 
    Size = new Size(10, 10) });            
    pf.Segments.Add(new ArcSegment() { SweepDirection = SweepDirection.Clockwise, 
    Point = new Point(15, 19.14), RotationAngle = 270, Size = new Size(10, 10), 
    IsLargeArc = true });           
    pf.Segments.Add(new ArcSegment() { SweepDirection = SweepDirection.Counterclockwise, 
    Point = new Point(10, 24.14), RotationAngle = 45, Size = new Size(10, 10) });            
    pg.Figures.Add(pf);           
    return pg;     
    }       
           
    static Random rnd = new Random();    
    public static Color GetRandomColor(byte a)    
    {
    return Color.FromArgb(a, (byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255)); 
    }      
    public static Color GetRandomColor(byte min, byte a)   
    {     
    return Color.FromArgb(a, (byte)(min + rnd.Next(255 - min)),   
    (byte)(min + rnd.Next(255 - min)), (byte)(min + rnd.Next(255 - min))); 
    }   
    }
            }
    

この手順では、マーカーバルーンの作成とマーカーのランダムな色付けを処理するコードファイルを追加しました。次の手順では、このアプリケーションを実行します。