私は、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
{
}
私の見解にはたくさんのコードがあるので、コードの他の部分を見る必要がある場合は、私に知らせてください。どんな助けでも大歓迎です。私が言ったように、私は以前にこのようなことをしたことがありません。ありがとうございました。