Skip to content

Commit

Permalink
WinUI: Multiple fixes in FindPlace sample (#1486)
Browse files Browse the repository at this point in the history
Co-authored-by: Hamish Duff <[email protected]>
  • Loading branch information
mstefarov and duffh authored Jul 22, 2024
1 parent 8ddbdcb commit 836d24e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
28 changes: 16 additions & 12 deletions src/MAUI/Maui.Samples/Samples/Search/FindPlace/FindPlace.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re

// Create the geocode parameters.
GeocodeParameters parameters = new GeocodeParameters();

// Request that the "Address" attribute is included with results, to display in callouts.
parameters.ResultAttributeNames.Add("Address");

try
{
// Get the MapPoint for the current search location.
Expand All @@ -146,6 +150,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
if (searchLocation != null)
{
parameters.PreferredSearchLocation = searchLocation;

// Raise MinScore to a non-zero value, otherwise PreferredSearchLocation has no effect.
parameters.MinScore = 1;
}

// Update the search area if desired.
Expand Down Expand Up @@ -174,24 +181,18 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re

// Create the GraphicsOverlay so that results can be drawn on the map.
GraphicsOverlay resultOverlay = new GraphicsOverlay();
var symbol = await GetPinSymbolAsync();


// Add each address to the map.
foreach (GeocodeResult location in locations)
{
// Get the Graphic to display.
Graphic point = await GraphicForPoint(location.DisplayLocation);
var point = new Graphic(location.DisplayLocation, symbol);

// Add the specific result data to the point.
point.Attributes["Match_Title"] = location.Label;

// Get the address for the point.
IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);

// Add the first suitable address if possible.
if (addresses.Any())
{
point.Attributes["Match_Address"] = addresses.First().Label;
}
point.Attributes["Match_Address"] = location.Attributes["Address"];

// Add the Graphic to the GraphicsOverlay.
resultOverlay.Graphics.Add(point);
Expand All @@ -212,7 +213,10 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
}
}

private async Task<Graphic> GraphicForPoint(MapPoint point)
/// <summary>
/// Creates and returns a "Pin" symbol used to mark search results on the MapView.
/// </summary>
private async Task<Esri.ArcGISRuntime.Symbology.Symbol> GetPinSymbolAsync()
{
// Get current assembly that contains the image.
Assembly currentAssembly = Assembly.GetExecutingAssembly();
Expand All @@ -230,7 +234,7 @@ private async Task<Graphic> GraphicForPoint(MapPoint point)
// is on the point rather than the image's true center.
pinSymbol.LeaderOffsetX = 30;
pinSymbol.OffsetY = 14;
return new Graphic(point, pinSymbol);
return pinSymbol;
}

private async void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.Maui.GeoViewInputEventArgs e)
Expand Down
25 changes: 12 additions & 13 deletions src/WPF/WPF.Viewer/Samples/Search/FindPlace/FindPlace.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,19 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
// Create the geocode parameters.
GeocodeParameters parameters = new GeocodeParameters();

// Request that the "Address" attribute is included with results, to display in callouts.
parameters.ResultAttributeNames.Add("Address");

// Get the MapPoint for the current search location.
MapPoint searchLocation = await GetSearchMapPoint(locationText);

// Update the geocode parameters if the map point is not null.
if (searchLocation != null)
{
parameters.PreferredSearchLocation = searchLocation;

// Raise MinScore to a non-zero value, otherwise PreferredSearchLocation has no effect.
parameters.MinScore = 1;
}

// Update the search area if desired.
Expand All @@ -179,24 +185,17 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re

// Create the GraphicsOverlay so that results can be drawn on the map.
GraphicsOverlay resultOverlay = new GraphicsOverlay();
var symbol = await GetPinSymbolAsync();

// Add each address to the map.
foreach (GeocodeResult location in locations)
{
// Get the Graphic to display.
Graphic point = await GraphicForPoint(location.DisplayLocation);
var point = new Graphic(location.DisplayLocation, symbol);

// Add the specific result data to the point.
point.Attributes["Match_Title"] = location.Label;

// Get the address for the point.
IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);

// Add the first suitable address if possible.
if (addresses.Any())
{
point.Attributes["Match_Address"] = addresses[0].Label;
}
point.Attributes["Match_Address"] = location.Attributes["Address"];

// Add the Graphic to the GraphicsOverlay.
resultOverlay.Graphics.Add(point);
Expand All @@ -218,9 +217,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
}

/// <summary>
/// Creates and returns a Graphic associated with the given MapPoint.
/// Creates and returns a "Pin" symbol used to mark search results on the MapView.
/// </summary>
private async Task<Graphic> GraphicForPoint(MapPoint point)
private async Task<Esri.ArcGISRuntime.Symbology.Symbol> GetPinSymbolAsync()
{
// Hold a reference to the picture marker symbol.
PictureMarkerSymbol pinSymbol;
Expand All @@ -245,7 +244,7 @@ private async Task<Graphic> GraphicForPoint(MapPoint point)
pinSymbol.OffsetY = 14;
}

return new Graphic(point, pinSymbol);
return pinSymbol;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using ArcGIS.Samples.Shared.Managers;
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Location;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.Tasks.Geocoding;
Expand Down Expand Up @@ -144,6 +145,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
// Create the geocode parameters
GeocodeParameters parameters = new GeocodeParameters();

// Request that the "Address" attribute is included with results, to display in callouts.
parameters.ResultAttributeNames.Add("Address");

try
{
// Get the MapPoint for the current search location
Expand All @@ -153,6 +157,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
if (searchLocation != null)
{
parameters.PreferredSearchLocation = searchLocation;

// Raise MinScore to a non-zero value, otherwise PreferredSearchLocation has no effect.
parameters.MinScore = 1;
}

// Update the search area if desired
Expand Down Expand Up @@ -181,24 +188,17 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re

// Create the GraphicsOverlay so that results can be drawn on the map
GraphicsOverlay resultOverlay = new GraphicsOverlay();
var symbol = await GetPinSymbolAsync();

// Add each address to the map
foreach (GeocodeResult location in locations)
{
// Get the Graphic to display
Graphic point = await GraphicForPoint(location.DisplayLocation);
var point = new Graphic(location.DisplayLocation, symbol);

// Add the specific result data to the point
point.Attributes["Match_Title"] = location.Label;

// Get the address for the point
IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);

// Add the first suitable address if possible
if (addresses.Any())
{
point.Attributes["Match_Address"] = addresses[0].Label;
}
point.Attributes["Match_Address"] = location.Attributes["Address"];

// Add the Graphic to the GraphicsOverlay
resultOverlay.Graphics.Add(point);
Expand All @@ -220,9 +220,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
}

/// <summary>
/// Creates and returns a Graphic associated with the given MapPoint
/// Creates and returns a "Pin" symbol used to mark search results on the MapView
/// </summary>
private async Task<Graphic> GraphicForPoint(MapPoint point)
private async Task<Esri.ArcGISRuntime.Symbology.Symbol> GetPinSymbolAsync()
{
// Get current assembly that contains the image
Assembly currentAssembly = GetType().GetTypeInfo().Assembly;
Expand All @@ -240,7 +240,7 @@ private async Task<Graphic> GraphicForPoint(MapPoint point)
// is on the point rather than the image's true center
pinSymbol.LeaderOffsetX = 30;
pinSymbol.OffsetY = 14;
return new Graphic(point, pinSymbol);
return pinSymbol;
}

/// <summary>
Expand Down

0 comments on commit 836d24e

Please sign in to comment.