Skip to content

Commit 836d24e

Browse files
mstefarovduffh
andauthored
WinUI: Multiple fixes in FindPlace sample (#1486)
Co-authored-by: Hamish Duff <[email protected]>
1 parent 8ddbdcb commit 836d24e

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

src/MAUI/Maui.Samples/Samples/Search/FindPlace/FindPlace.xaml.cs

+16-12
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
137137

138138
// Create the geocode parameters.
139139
GeocodeParameters parameters = new GeocodeParameters();
140+
141+
// Request that the "Address" attribute is included with results, to display in callouts.
142+
parameters.ResultAttributeNames.Add("Address");
143+
140144
try
141145
{
142146
// Get the MapPoint for the current search location.
@@ -146,6 +150,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
146150
if (searchLocation != null)
147151
{
148152
parameters.PreferredSearchLocation = searchLocation;
153+
154+
// Raise MinScore to a non-zero value, otherwise PreferredSearchLocation has no effect.
155+
parameters.MinScore = 1;
149156
}
150157

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

175182
// Create the GraphicsOverlay so that results can be drawn on the map.
176183
GraphicsOverlay resultOverlay = new GraphicsOverlay();
184+
var symbol = await GetPinSymbolAsync();
185+
177186

178187
// Add each address to the map.
179188
foreach (GeocodeResult location in locations)
180189
{
181190
// Get the Graphic to display.
182-
Graphic point = await GraphicForPoint(location.DisplayLocation);
191+
var point = new Graphic(location.DisplayLocation, symbol);
183192

184193
// Add the specific result data to the point.
185194
point.Attributes["Match_Title"] = location.Label;
186-
187-
// Get the address for the point.
188-
IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);
189-
190-
// Add the first suitable address if possible.
191-
if (addresses.Any())
192-
{
193-
point.Attributes["Match_Address"] = addresses.First().Label;
194-
}
195+
point.Attributes["Match_Address"] = location.Attributes["Address"];
195196

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

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

236240
private async void MyMapView_GeoViewTapped(object sender, Esri.ArcGISRuntime.Maui.GeoViewInputEventArgs e)

src/WPF/WPF.Viewer/Samples/Search/FindPlace/FindPlace.xaml.cs

+12-13
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,19 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
147147
// Create the geocode parameters.
148148
GeocodeParameters parameters = new GeocodeParameters();
149149

150+
// Request that the "Address" attribute is included with results, to display in callouts.
151+
parameters.ResultAttributeNames.Add("Address");
152+
150153
// Get the MapPoint for the current search location.
151154
MapPoint searchLocation = await GetSearchMapPoint(locationText);
152155

153156
// Update the geocode parameters if the map point is not null.
154157
if (searchLocation != null)
155158
{
156159
parameters.PreferredSearchLocation = searchLocation;
160+
161+
// Raise MinScore to a non-zero value, otherwise PreferredSearchLocation has no effect.
162+
parameters.MinScore = 1;
157163
}
158164

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

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

183190
// Add each address to the map.
184191
foreach (GeocodeResult location in locations)
185192
{
186193
// Get the Graphic to display.
187-
Graphic point = await GraphicForPoint(location.DisplayLocation);
194+
var point = new Graphic(location.DisplayLocation, symbol);
188195

189196
// Add the specific result data to the point.
190197
point.Attributes["Match_Title"] = location.Label;
191-
192-
// Get the address for the point.
193-
IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);
194-
195-
// Add the first suitable address if possible.
196-
if (addresses.Any())
197-
{
198-
point.Attributes["Match_Address"] = addresses[0].Label;
199-
}
198+
point.Attributes["Match_Address"] = location.Attributes["Address"];
200199

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

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

248-
return new Graphic(point, pinSymbol);
247+
return pinSymbol;
249248
}
250249

251250
/// <summary>

src/WinUI/ArcGIS.WinUI.Viewer/Samples/Search/FindPlace/FindPlace.xaml.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using ArcGIS.Samples.Shared.Managers;
1111
using Esri.ArcGISRuntime.Data;
1212
using Esri.ArcGISRuntime.Geometry;
13+
using Esri.ArcGISRuntime.Location;
1314
using Esri.ArcGISRuntime.Mapping;
1415
using Esri.ArcGISRuntime.Symbology;
1516
using Esri.ArcGISRuntime.Tasks.Geocoding;
@@ -144,6 +145,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
144145
// Create the geocode parameters
145146
GeocodeParameters parameters = new GeocodeParameters();
146147

148+
// Request that the "Address" attribute is included with results, to display in callouts.
149+
parameters.ResultAttributeNames.Add("Address");
150+
147151
try
148152
{
149153
// Get the MapPoint for the current search location
@@ -153,6 +157,9 @@ private async Task UpdateSearch(string enteredText, string locationText, bool re
153157
if (searchLocation != null)
154158
{
155159
parameters.PreferredSearchLocation = searchLocation;
160+
161+
// Raise MinScore to a non-zero value, otherwise PreferredSearchLocation has no effect.
162+
parameters.MinScore = 1;
156163
}
157164

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

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

185193
// Add each address to the map
186194
foreach (GeocodeResult location in locations)
187195
{
188196
// Get the Graphic to display
189-
Graphic point = await GraphicForPoint(location.DisplayLocation);
197+
var point = new Graphic(location.DisplayLocation, symbol);
190198

191199
// Add the specific result data to the point
192200
point.Attributes["Match_Title"] = location.Label;
193-
194-
// Get the address for the point
195-
IReadOnlyList<GeocodeResult> addresses = await _geocoder.ReverseGeocodeAsync(location.DisplayLocation);
196-
197-
// Add the first suitable address if possible
198-
if (addresses.Any())
199-
{
200-
point.Attributes["Match_Address"] = addresses[0].Label;
201-
}
201+
point.Attributes["Match_Address"] = location.Attributes["Address"];
202202

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

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

246246
/// <summary>

0 commit comments

Comments
 (0)