我使用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);
}
现在我将上面的代码和收到的信息传递给下面的代码(Mapping Service):
[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
{
}
我的视图中有很多代码,所以如果您需要查看其他部分代码,请告诉我。任何帮助将非常感激。就像我说的那样,我以前从未真正做过这样的事情。感谢您。
答案 0 :(得分:0)
如果您需要在服务中使用EsriMapView对象,则需要通过MEF导入它。您可以创建导入构造函数或创建所需类型的公共属性,并使用Import属性对其进行标记。
e.g。
[Import]
public EsriMapView MapView { get; set; }
或者,如果您需要EsriMapView类中的服务,那么您将在该类中创建一个属性,导入该服务。
e.g。
[Import]
public IMappingService MapViewService { get; set; }
对于导入构造函数,您将执行以下操作
[ImportingConstructor]
public EsriMapView([Import] IMappingService mappingService)
{
}
理想情况下,在标记要导入和导出的项目时,通常会使用接口。允许您更换不同的实现以进行测试等。
阅读MEF文档以获取更多相关信息。