private void LocatorTask_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 = LayoutRoot.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;
}
}