1

ユーザーコントロールをロードすると、マップレイヤーにグラフィックを追加するプログラムがあります(マップに関する知識はあまり必要ありません。問題はループロジックにあると思います)。

これが私のコードです:

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
    con = new System.Data.SqlClient.SqlConnection();

    con.ConnectionString = "Data Source=USSW7DEVWS16\\DEVELOPER;Initial Catalog=acrGIS;Integrated Security=True";

    con.Open();
    using (SqlCommand command = new SqlCommand("SELECT * FROM arcObjects", con))
    {
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            int ID = reader.GetInt32(0);
            Object = reader.GetString(1);
            OComment = reader.GetString(2);
            OStreet = reader.GetString(3);
            OCity = reader.GetString(4);
            OState = reader.GetString(5);
            OZip = reader.GetString(6);
            OSpec = reader.GetString(7);

            arcObjects.Add(new arcObject() { Object_Num = Object, Comments = OComment, Street = OStreet, City = OCity, State = OState, Zip = OZip, Spec = OSpec });
        }

        con.Close();
        foreach (arcObject objects in arcObjects)
        {
            Locator InitialLocatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer");
            InitialLocatorTask.AddressToLocationsCompleted += InitialLocatorTask_AddressToLocatonCompleted;
            InitialLocatorTask.Failed += InitialLocatorTask_Failed;
            AddressToLocationsParameters addressParams = new AddressToLocationsParameters();

            Dictionary<string, string> address = addressParams.Address;

            address.Add("Street", objects.Street);
            address.Add("City", objects.City);
            address.Add("State", objects.State);
            address.Add("Zip", objects.Zip);

            InitialLocatorTask.AddressToLocationsAsync(addressParams);
        }
    }
}

private void InitialLocatorTask_AddressToLocatonCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs args)
{
    con = new System.Data.SqlClient.SqlConnection();

    con.ConnectionString = "Data Source=USSW7DEVWS16\\DEVELOPER;Initial Catalog=acrGIS;Integrated Security=True";

    con.Open();
    using (SqlCommand command = new SqlCommand("SELECT * FROM arcObjects", con))
    {
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            int ID = reader.GetInt32(0);
            Object = reader.GetString(1);
            OComment = reader.GetString(2);
            OStreet = reader.GetString(3);
            OCity = reader.GetString(4);
            OState = reader.GetString(5);
            OZip = reader.GetString(6);
            OSpec = reader.GetString(7);

            CandidateListBox.Items.Clear();

            List<AddressCandidate> returnedCandidates = args.Results;

            foreach (AddressCandidate candidate in returnedCandidates)
            {
                if (candidate.Score >= 80)
                {
                    CandidateListBox.Items.Add(candidate.Address);

                    if (OSpec == "Meter")
                    {
                        Graphic graphic = new Graphic()
                        {
                            Symbol = AddressLayout.Resources["WaterMeterRenderer"] as ESRI.ArcGIS.Client.Symbols.Symbol,
                            Geometry = candidate.Location
                        };

                        string address2 = OCity + ", " + OState + " " + OZip;

                        graphic.Attributes.Add("MeterNum", Object);
                        graphic.Attributes.Add("Comment", OComment);
                        graphic.Attributes.Add("Address1", OStreet);
                        graphic.Attributes.Add("Address2", address2);

                        if (candidate.Location.SpatialReference == null)
                        {
                            candidate.Location.SpatialReference = new SpatialReference(4326);
                        }

                        // if (!candidate.Location.SpatialReference.Equals(MyMap.SpatialReference))
                        //{
                        // if (MyMap.SpatialReference.Equals(new SpatialReference(102100)) && candidate.Location.SpatialReference.Equals(new SpatialReference(4326)))
                        // graphic.Geometry = _mercator.FromGeographic(graphic.Geometry);
                        // else if (MyMap.SpatialReference.Equals(new SpatialReference(4326)) && candidate.Location.SpatialReference.Equals(new SpatialReference(102100)))
                        // graphic.Geometry = _mercator.ToGeographic(graphic.Geometry);
                        // else if (MyMap.SpatialReference != new SpatialReference(4326))
                        // {
                        GeometryService geometryService = new  
GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

                        geometryService.ProjectCompleted += (s, a) =>
                        {
                            graphic.Geometry = a.Results[0].Geometry;
                        };

                        geometryService.Failed += (s, a) =>
                        {
                            MessageBox.Show("Projection error: " + a.Error.Message);
                        };

                        //geometryService.ProjectAsync(new List<Graphic> { graphic }, MyMap.SpatialReference);
                        //    }
                        //}

                        FeatureLayer graphicsLayer = MyMap.Layers["WaterMeterLayer"] as FeatureLayer;
                        graphicsLayer.Graphics.Add(graphic);
                        //return;
                    }

                    if (OSpec == "Lot")
                    {
                        Graphic graphic = new Graphic()
                        {
                            Symbol = AddressLayout.Resources["LotRenderer"] as ESRI.ArcGIS.Client.Symbols.Symbol,
                            Geometry = candidate.Location
                        };

                        string address2 = OCity + ", " + OState + " " + OZip;

                        graphic.Attributes.Add("MeterNum", Object);
                        graphic.Attributes.Add("Comment", OComment);
                        graphic.Attributes.Add("Address1", OStreet);
                        graphic.Attributes.Add("Address2", address2);

                        if (candidate.Location.SpatialReference == null)
                        {
                            candidate.Location.SpatialReference = new SpatialReference(4326);
                        }

                        //if (!candidate.Location.SpatialReference.Equals(MyMap.SpatialReference))
                        // {
                        // if (MyMap.SpatialReference.Equals(new SpatialReference(102100)) && candidate.Location.SpatialReference.Equals(new SpatialReference(4326)))
                        // graphic.Geometry = _mercator.FromGeographic(graphic.Geometry);
                        // else if (MyMap.SpatialReference.Equals(new SpatialReference(4326)) && candidate.Location.SpatialReference.Equals(new SpatialReference(102100)))
                        // graphic.Geometry = _mercator.ToGeographic(graphic.Geometry);
                        //    else if (MyMap.SpatialReference != new SpatialReference(4326))
                        //    {
                        GeometryService geometryService = new GeometryService("http://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

                         geometryService.ProjectCompleted += (s, a) =>
                         {
                             graphic.Geometry = a.Results[0].Geometry;
                         };

                         geometryService.Failed += (s, a) =>
                         {
                             MessageBox.Show("Projection error: " + a.Error.Message);
                         };

                         //geometryService.ProjectAsync(new List<Graphic> { graphic }, MyMap.SpatialReference);
                         // }
                         // }

                         FeatureLayer graphicsLayer = MyMap.Layers["LotLayer"] as FeatureLayer;
                         graphicsLayer.Graphics.Add(graphic);
                         // return;
                     }
                 }
             }
         }
     }
     con.Close();
 }


public void InitialLocatorTask_Failed(object sender, TaskFailedEventArgs e)
{
    MessageBox.Show("Locator service failed: " + e.Error);
}

私はそれが何が起こっているかを知っています、UserControl_Loadedのforeachループは辞書に物事を追加していて、InitailTask​​ToAddress_Completedのwhileループは何度も何度も実行されます(正確には9回)。問題は、すべてのグラフィックが正しい場所に配置されている間、それらのグラフィックの属性はすべて同じであるということです...

何らかの理由でそれはそれらを取得していません。私に何かを片付けてほしいのなら、私に知らせてください。また、私のXAMLをご覧になりたい場合は、それをお見せすることもできます。私はC#とWPF、およびarcGISを使用しています。ご協力ありがとうございました。

4

0 に答える 0