2012-07-12 95 views
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,但我也不知道這將如何發揮作用。 這裏是視圖模型的樣子截至目前:

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

    } 

有一個在我看來,大量的代碼,所以如果有,你需要看到的只是讓我知道代碼的其他部分。任何幫助將非常感激。就像我說的,我以前從來沒有真正做過這樣的事情。謝謝。

+0

你應該刪除MVVM標籤,導致多數民衆贊成而不是MVVM;)你的看法是充滿視圖模型代碼 – blindmeis 2012-07-13 05:41:33

回答

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