1

我使用 arcGIS、C#、WPF 和 MEF 创建了一个地图应用程序。我实际上对所有这些让我的项目相当困难的事情都很陌生。我以自己想要的方式拥有应用程序,然后在需要将其实现到代码中时遇到了问题。

这是我的应用程序的部分代码:

[Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public partial class EsriMapView : DialogWindowBase
    {

        string Comments;
        string MeterNumMessage;
        GraphicsLayer _candidateGraphicsLayer;
        private static ESRI.ArcGIS.Client.Projection.WebMercator _mercator =
                new ESRI.ArcGIS.Client.Projection.WebMercator();
        bool blnAddInformationClicked = false;
        bool blnEnterAddressClicked = false;
        string Specification = "Object Number: ";
        string addressCombined;
        string Object;
        string OComment;
        string OStreet;
        string OCity;
        string OState;
        string OZip;
        string OSpec;
        string GAttributes;
        string splitstring;
        int i = 0;
        string GStreet;
        string GObject;
        string GCity;
        string GState;
        string GZip;
        string GComment;
        string GSpec;
        Dictionary<string, string> Information = new Dictionary<string, string>();
        Locator locatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US/GeocodeServer");
        Locator AddlocatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer");




        class arcObject
        {
            public int ID { get; set; }
            public string Object_Num { get; set; }
            public string Comments { get; set; }
            public string Street { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Zip { get; set; }
            public string Spec { get; set; }
            public override string ToString()
            {
                return string.Format("Object Number: {0}, Comments: {1}, Street: {2}, City: {3}, State: {4}, Zip: {5}, Spec: {6}", Object_Num, Comments, Street, City, State, Zip, Spec);
            }
        }


        System.Data.SqlClient.SqlConnection con;

        List<arcObject> arcObjects = new List<arcObject>();


        public EsriMapView()
        {
            InitializeComponent();  

         AddressGrid.Visibility = Visibility.Collapsed;
            MeterLotGrid.Visibility = Visibility.Collapsed;
            InformationGrid.Visibility = Visibility.Collapsed;
            FindPortalGrid.Visibility = Visibility.Collapsed;
            InstructionGrid.Visibility = Visibility.Collapsed;
            AddPortalGrid.Visibility = Visibility.Collapsed;
            UpdateObjectGrid.Visibility = Visibility.Collapsed;
            UpdateInformationGrid.Visibility = Visibility.Collapsed;
            ViewInfoGrid.Visibility = Visibility.Collapsed;

            ESRI.ArcGIS.Client.Geometry.Envelope initialExtent =
                                     new ESRI.ArcGIS.Client.Geometry.Envelope(
                             _mercator.FromGeographic(
                                     new ESRI.ArcGIS.Client.Geometry.MapPoint(-88.320026, 36.607915)) as MapPoint,
                             _mercator.FromGeographic(
                                     new ESRI.ArcGIS.Client.Geometry.MapPoint(-88.320026, 36.607915)) as MapPoint);

            initialExtent.SpatialReference = new SpatialReference(102100);



            _candidateGraphicsLayer = MyMap.Layers["CandidateGraphicsLayer"] as GraphicsLayer;
        }

        private void FindAddressButton_Click(object sender, RoutedEventArgs e)
        {
            bool NoNulls;



            if ((Address.Text == "") | (City.Text == "") | (State.Text == "") | (Zip.Text == ""))
            {
                NoNulls = false;
                MessageBox.Show("All Fields Must Be Completed!");
            }
            else
            {
                NoNulls = true;
            }

            if (NoNulls)
            {
                Locator FindlocatorTask = new Locator("http://tasks.arcgisonline.com/ArcGIS/rest/services/Locators/TA_Streets_US_10/GeocodeServer");
                FindlocatorTask.AddressToLocationsCompleted += FindLocatorTask_AddressToLocationsCompleted;
                FindlocatorTask.Failed += FindLocatorTask_Failed;
                AddressToLocationsParameters addressParams = new AddressToLocationsParameters();
                Dictionary<string, string> address = addressParams.Address;

                if (!string.IsNullOrEmpty(Address.Text))
                    address.Add("Street", Address.Text);
                if (!string.IsNullOrEmpty(City.Text))
                    address.Add("City", City.Text);
                if (!string.IsNullOrEmpty(State.Text))
                    address.Add("State", State.Text);
                if (!string.IsNullOrEmpty(Zip.Text))
                    address.Add("ZIP", Zip.Text);

                FindlocatorTask.AddressToLocationsAsync(addressParams);
                PortalGrid.Visibility = Visibility.Visible;
                AddressGrid.Visibility = Visibility.Collapsed;
                InstructionGrid.Visibility = Visibility.Collapsed;
            }

        }

        private void AddMarkerButton_Click(object sender, RoutedEventArgs e)
        {
            InformationGrid.Visibility = Visibility.Visible;

        }


        private void FindLocatorTask_AddressToLocationsCompleted(object sender, ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs args)
        {
            _candidateGraphicsLayer.ClearGraphics();
            CandidateListBox.Items.Clear();

            List<AddressCandidate> returnedCandidates = args.Results;

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

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

                    graphic.Attributes.Add("Address", candidate.Address);

                    string latlon = String.Format("{0}, {1}", candidate.Location.X, candidate.Location.Y);
                    graphic.Attributes.Add("LatLon", latlon);

                    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);
                        }
                    }

                    _candidateGraphicsLayer.Graphics.Add(graphic);
                }
            }

            if (_candidateGraphicsLayer.Graphics.Count > 0)
            {
                CandidatePanelGrid.Visibility = Visibility.Visible;
                CandidateListBox.SelectedIndex = 0;
            }
        }

        void _candidateListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = (sender as ListBox).SelectedIndex;
            if (index >= 0)
            {
                MapPoint candidatePoint = _candidateGraphicsLayer.Graphics[index].Geometry as MapPoint;
                double displaySize = MyMap.MinimumResolution * 30;

                ESRI.ArcGIS.Client.Geometry.Envelope displayExtent = new ESRI.ArcGIS.Client.Geometry.Envelope(
                        candidatePoint.X - (displaySize / 2),
                        candidatePoint.Y - (displaySize / 2),
                        candidatePoint.X + (displaySize / 2),
                        candidatePoint.Y + (displaySize / 2));

                MyMap.ZoomTo(displayExtent);
            }
        }

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

现在我将上面的代码和接收到的信息传递到以下代码(映射服务)中:

[Description("ESRI Mapping Service")]
    [Export(typeof(IMappingService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class EsriMappingService : IMappingService
    {
        #region Private Fields
        private readonly IUserInteractionService uis = AllianceApp.Container.GetExportedValue<IUserInteractionService>();
        #endregion

        #region IMapping Service Inteface


        void IMappingService.GetLatLong()
        {
            //TODO should look at reusing the implementation from the bing mapping service for getting current LatLong and should look at moving that out
            throw new NotImplementedException();
        }

        void IMappingService.ShowBlankMap(int? initialZoom, double? latitude, double? longitude)
        {
            throw new NotImplementedException();
        }

        void IMappingService.ShowPoints(string point, string pointTitle, string pointDetails, string pointLinkID, bool? zoomToBounds, bool? showLocation)
        {
            throw new NotImplementedException();
        }

        void IMappingService.ShowPoints(string point, string pointTitle, string pointDetails, string pointLinkID, bool? zoomToBounds)
        {
            throw new NotImplementedException();
        }

        void IMappingService.ShowPoints(bool? zoomToBounds)
        {
            this.ShowMap();
        }

        void IMappingService.ShowRoute(string start, string end, bool? showpoints, bool? showLocation)
        {
            throw new NotImplementedException();
        }

        void IMappingService.ShowRoute(string start, string end, bool? showpoints)
        {
            throw new NotImplementedException();
        }

        double? IMappingService.gpsLat
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        double? IMappingService.gpsLong
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        private void ShowMap()
        {
            EsriMapView mv = new EsriMapView();
            uis.ShowDialog(mv);
        }
    }
}

代码是为我设置的,这让我陷入了循环。我只需要能够使用下面的代码行在地图上硬编码一个点,但它可能需要不同的参数:

mapService.ShowPoints("1409 Fleetwood Dr, Murray, KY", "test Location", "Details", "foo", true);

我只需要知道是否需要向 EsriMapView(第一段代码)添加任何内容。我还为我设置了一个 ViewModel,但我也不知道它会如何发挥作用。这是 ViewModel 目前的样子:

[Export]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class EsriMapViewModel : ViewModelBase
    {

    }

在我看来有很多代码,所以如果您需要查看代码的其他部分,请告诉我。任何帮助将不胜感激。就像我说的,我以前从未真正做过这样的事情。谢谢你。

4

1 回答 1

0

如果您需要在服务中使用 EsriMapView 对象,则需要通过 MEF 导入它。您可以创建导入构造函数或创建所需类型的公共属性,并使用 Import 特性对其进行标记。

例如

 [Import]
    public EsriMapView MapView { get; set; }

或者,如果您需要 EsriMapView 类中的服务,那么您将在该类中创建一个属性,导入该服务。

例如

[Import]
        public IMappingService MapViewService { get; set; }

对于导入构造函数,您将执行以下操作

[ImportingConstructor]
    public EsriMapView([Import] IMappingService mappingService)
    {
    }

理想情况下,您通常会在为导入和导出标记项目时使用接口。允许您换出不同的实现以进行测试等。

阅读 MEF 文档以获取有关此的更多信息。

MEF

于 2012-07-12T14:50:33.103 回答