public static async Task <IReadOnlyList <GeocodeResult> > ReverseGeocodeAsync(MapPoint location, double maxDistance = 200) { await _geocoder.LoadAsync().ConfigureAwait(false); var parameters = new ReverseGeocodeParameters() { MaxDistance = maxDistance }; return(await _geocoder.ReverseGeocodeAsync(location, parameters).ConfigureAwait(false)); }
private async void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.Xamarin.Forms.GeoViewInputEventArgs e) { try { // Clear existing callout and graphics. MyMapView.DismissCallout(); MyMapView.GraphicsOverlays[0].Graphics.Clear(); // Add a graphic for the tapped point. Graphic pinGraphic = await GraphicForPoint(e.Location); MyMapView.GraphicsOverlays[0].Graphics.Add(pinGraphic); // Reverse geocode to get addresses. ReverseGeocodeParameters parameters = new ReverseGeocodeParameters(); parameters.ResultAttributeNames.Add("*"); parameters.MaxResults = 1; IReadOnlyList <GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(e.Location, parameters); // Skip if there are no results. if (!addresses.Any()) { await Application.Current.MainPage.DisplayAlert("No results", "No results found.", "OK"); return; } // Get the first result. GeocodeResult address = addresses.First(); // Use the address as the callout title. string calloutTitle = address.Attributes["Street"].ToString(); string calloutDetail = address.Attributes["City"] + ", " + address.Attributes["State"] + " " + address.Attributes["ZIP"]; // Define the callout. CalloutDefinition calloutBody = new CalloutDefinition(calloutTitle, calloutDetail); // Show the callout on the map at the tapped location. MyMapView.ShowCalloutForGeoElement(pinGraphic, e.Position, calloutBody); } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("Error", ex.ToString(), "OK"); } }
/// <summary> /// Reverse geocode au clic sur la carte /// </summary> /// <param name="sender"></param> /// <param name="e"></param> async void mapView1_GeoViewTapped(object sender, GeoViewInputEventArgs e) { // on set les paramètres du reverse geocode ReverseGeocodeParameters geocodeParams = new ReverseGeocodeParameters(); geocodeParams.MaxDistance = 800; geocodeParams.MaxResults = 10; // on effectue le reverse geocode sur l'emplacement cliqué IReadOnlyList <GeocodeResult> resultList = await myLocator.ReverseGeocodeAsync(e.Location, geocodeParams); // si il y a un résultat, on affiche le premier if (resultList.Count > 0) { reverseGeocodeLabel.Visibility = Visibility.Visible; reverseGeocodeLabel.Content = "Magasin : " + resultList[0].Label; } else { reverseGeocodeLabel.Visibility = Visibility.Hidden; } }
private async Task UpdateGeocodeAsync(MapPoint location) { var layer = FindGraphicsOverlay("geocode"); if (layer.Graphics.Count == 0) { layer.Graphics.Add(new Graphic()); } layer.Graphics[0].Geometry = location; if (m_locatorTask == null) { m_locatorTask = await GetLocator(); } GeocodeResult result = null; try { var p = GeometryEngine.Project(location, m_locatorTask.LocatorInfo.SpatialReference) as MapPoint; var param = new ReverseGeocodeParameters() { MaxDistance = 500 }; param.ResultAttributeNames.Add("*"); var results = await m_locatorTask.ReverseGeocodeAsync(p, param); result = results?.FirstOrDefault(); } catch { } if (result != null) { ReverseGeocodeResult = string.Join("\n", result.Attributes.Values.ToArray()); } else { ReverseGeocodeResult = "No result"; } }
private async void MyMapView_GeoViewTapped(object sender, GeoViewInputEventArgs e) { try { // Clear existing callout and graphics. MyMapView.DismissCallout(); MyMapView.GraphicsOverlays[0].Graphics.Clear(); // Add a graphic for the tapped point. Graphic pinGraphic = await GraphicForPoint(e.Location); MyMapView.GraphicsOverlays[0].Graphics.Add(pinGraphic); // Reverse geocode to get addresses. ReverseGeocodeParameters parameters = new ReverseGeocodeParameters(); parameters.ResultAttributeNames.Add("*"); parameters.MaxResults = 1; IReadOnlyList <GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(e.Location, parameters); // Get the first result. GeocodeResult address = addresses.First(); // Use the address as the callout title. string calloutTitle = address.Attributes["Street"].ToString(); string calloutDetail = address.Attributes["City"] + ", " + address.Attributes["State"] + " " + address.Attributes["ZIP"]; // Define the callout. CalloutDefinition calloutBody = new CalloutDefinition(calloutTitle, calloutDetail); // Show the callout on the map at the tapped location. MyMapView.ShowCalloutForGeoElement(pinGraphic, e.Position, calloutBody); } catch (Exception ex) { await new MessageDialog2(ex.ToString(), "Error").ShowAsync(); } }
/// <summary> /// Defines the method to be called when the command is invoked. /// </summary> /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param> public void Execute(object parameter) { // The parameter should be an ESRI MapPoint. var location = parameter as Esri.ArcGISRuntime.Geometry.MapPoint; #region Sanity Check if (location == null) { MessageBox.Show( "A reverse geocode requires a location.", "No Location", MessageBoxButton.OK, MessageBoxImage.Error); return; } #endregion // We'll need access to the map view model. var mapViewModel = MapViewModel.Current; // Clear the list of reverse geocodes. mapViewModel.ReverseGeocodeResults.Clear(); // We need the ESRI locator. var locatorTask = MapViewModel.Current.MobileMapPackage.LocatorTask; // Apply the Workaround, Part 1... ReverseGeocodeParameters rgParams = new ReverseGeocodeParameters(); rgParams.MaxDistance = 10; rgParams.OutputSpatialReference = mapViewModel.MapView.Map.SpatialReference; rgParams.MaxResults = 5; rgParams.ResultAttributeNames.Add("*"); // let's try a reverse geocode... var results = Task.Run(async() => { return(await locatorTask.ReverseGeocodeAsync(location, rgParams)); }).Result; // Examine each of the results. foreach (var result in results) { /** * To those who want to troubleshoot... place a breakpoint * here to examine the result returned by the LocatorTask. */ // Apply the Workaround, Part 2... // (When we apply the parameters above, it seems that we get // additional information in the Attributes collection. (The // "Label" property still contains an empty string.) var labelAsSeenInAttributes = string.Empty; if (result.Attributes.Keys.Contains("Street")) { labelAsSeenInAttributes = result.Attributes["Street"] as string; } Console.WriteLine($"Label={result.Label}"); Console.WriteLine($"Score={result.Score}"); // Construct a label. string label = string.IsNullOrWhiteSpace(labelAsSeenInAttributes) ? "The label came back as an empty string. Why is that?" : labelAsSeenInAttributes; // Create an instance of our own GeocodeResult to hold the // values from ESRI's GeocodeResult. Model.GeocodeResult resultNode = new Model.GeocodeResult(result); // Place the reverse geocode results into the collection so // they may be viewed. mapViewModel.ReverseGeocodeResults.Add( new ObjectNode($"Label={label}", resultNode)); } }