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