This sample shows how to use the Portal API to search public webmaps matching the search criterion on ArcGIS Online. To use the sample, click on Initialize button to initialize the portal. Once the portal is initialized it is available to perform searches and queries. Enter the search parameters and click Find WebMaps. Webmaps matching the search parameters are listed and are marked on the Map using a GraphicsLayer.
<UserControlx:Class="ArcGISWPFSDK.PortalSpatialSearch"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:esri="http://schemas.esri.com/arcgis/client/2009"><Gridx:Name="LayoutRoot"><Grid.Resources><!-- Webmap Symbol--><esri:PictureMarkerSymbolx:Key="MyWebMapMarkerSymbol"Source="/Assets/Images/webmap.png"/><esri:SimpleRendererx:Key="MyGraphicsLayerRenderer"Symbol="{StaticResource MyWebMapMarkerSymbol}"/></Grid.Resources><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinitionWidth="285"/></Grid.ColumnDefinitions><!-- Left column, map with search results as graphics --><esri:Mapx:Name="MyMap"UseAcceleratedDisplay="True"WrapAround="True"Extent="-10827200,3228880,-10292800,3604400"Grid.Column="0"><esri:ArcGISTiledMapServiceLayerUrl="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/><esri:GraphicsLayerID="MyGraphicsLayer"Renderer="{StaticResource MyGraphicsLayerRenderer}"MouseLeftButtonUp="GraphicsLayer_MouseLeftButtonUp"PropertyChanged="GraphicsLayer_PropertyChanged"/></esri:Map><!-- Right column, search inputs and result items --><BorderGrid.Column="1"BorderBrush="Black"BorderThickness="1"><GridBackground="White"><Grid.ColumnDefinitions><ColumnDefinitionMaxWidth="110"/><ColumnDefinitionWidth="*"/></Grid.ColumnDefinitions><Grid.RowDefinitions><RowDefinitionMaxHeight="25"/><RowDefinitionMaxHeight="30"/><RowDefinitionMaxHeight="30"/><RowDefinitionMaxHeight="35"/><RowDefinitionMaxHeight="2"/><RowDefinitionMaxHeight="30"/><RowDefinition/></Grid.RowDefinitions><TextBlockGrid.Row="0"Grid.ColumnSpan="2"Margin="5"Text="Search for web maps within the current map extent"VerticalAlignment="Center"/><TextBlockText="Query string:"VerticalAlignment="Center"Margin="5"Grid.Row="1"Grid.Column="0"/><TextBoxx:Name="searchText"Text="Gulf"HorizontalAlignment="Left"Width="150"Grid.Row="1"Grid.Column="1"Margin="5"/><TextBlockText="Result limit:"VerticalAlignment="Center"Margin="5"Grid.Row="2"Grid.Column="0"/><TextBoxx:Name="resultLimit"Text="15"HorizontalAlignment="Left"Width="150"Grid.Row="2"Grid.Column="1"Margin="5"/><ButtonMargin="5"Grid.Row="3"Grid.ColumnSpan="2"Content="Find WebMaps"Width="100"VerticalAlignment="Center"HorizontalAlignment="Left"Click="FindWebMapsButton_Click"/><BorderBorderBrush="Black"BorderThickness="1"Grid.Row="4"Grid.ColumnSpan="2"/><TextBlockGrid.Row="5"Grid.ColumnSpan="2"HorizontalAlignment="Left"Text="Search Results"Margin="5"FontSize="12"VerticalAlignment="Center"FontWeight="Bold"/><ListBoxx:Name="WebMapsListBox"Grid.Row="6"Grid.ColumnSpan="2"><ListBox.ItemTemplate><DataTemplate><StackPanelOrientation="Vertical"Margin="2"Width="210"><ImageSource="{Binding ThumbnailUri}"Margin="5"Stretch="UniformToFill"MouseLeftButtonUp="Image_MouseLeftButtonUp"/><TextBlockTextWrapping="Wrap"Text="{Binding Title, StringFormat='Title: {0}'}"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Border></Grid></UserControl>
using System.Windows;
using System.Windows.Controls;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Portal;
using System;
using System.Windows.Documents;
using System.Diagnostics;
using ESRI.ArcGIS.Client.WebMap;
using System.Linq;
namespace ArcGISWPFSDK
{
publicpartialclass PortalSpatialSearch : UserControl
{
ArcGISPortal arcgisPortal;
ESRI.ArcGIS.Client.Projection.WebMercator mercator =
new ESRI.ArcGIS.Client.Projection.WebMercator();
GraphicsLayer webmapGraphicsLayer;
public PortalSpatialSearch()
{
InitializeComponent();
// Search public web maps on www.arcgis.com
arcgisPortal = new ArcGISPortal() { Url = "http://www.arcgis.com/sharing/rest" };
webmapGraphicsLayer = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
}
privatevoid FindWebMapsButton_Click(object sender, RoutedEventArgs e)
{
webmapGraphicsLayer.Graphics.Clear();
// Search envelope must be in geographic (WGS84). Convert the current map extent from Web Mercator// to geographic.
ESRI.ArcGIS.Client.Geometry.Geometry geom = mercator.ToGeographic(MyMap.Extent);
SpatialSearchParameters parameters = new SpatialSearchParameters
{
Limit = String.IsNullOrEmpty(resultLimit.Text) == true ? 15 : Convert.ToInt32(resultLimit.Text),
SearchExtent = geom.Extent,
QueryString = String.Format("{0} And type:Web Map", searchText.Text)
};
arcgisPortal.SearchItemsAsync(parameters, (result, error) =>
{
if (error == null)
{
// Set the ItemsSource for the Listbox to an IEnumerable of ArcGISPortalItems. // Bindings setup in the Listbox item template in XAML will enable the discovery and // display individual result items.
WebMapsListBox.ItemsSource = result.Results;
// For each web map returned, add the center (point) of the web map extent as a graphic.// Add the ArcGISPortalItem instance as an attribute. This will be used to select graphics// in the map. foreach (var item in result.Results)
{
Graphic graphic = new Graphic();
graphic.Attributes.Add("PortalItem", item);
MapPoint extentCenter = item.Extent.GetCenter();
graphic.Geometry = new MapPoint(extentCenter.X, extentCenter.Y, new SpatialReference(4326));
webmapGraphicsLayer.Graphics.Add(graphic);
}
}
});
}
// Click on a graphic in the map and select it. Scroll to the web map item in the Listbox. privatevoid GraphicsLayer_MouseLeftButtonUp(object sender, GraphicMouseButtonEventArgs e)
{
webmapGraphicsLayer.ClearSelection();
Graphic graphic = e.Graphic;
graphic.Selected = true;
ArcGISPortalItem portalitem = graphic.Attributes["PortalItem"] as ArcGISPortalItem;
WebMapsListBox.SelectedItem = portalitem;
WebMapsListBox.ScrollIntoView(portalitem);
}
// Click on a web map item in the Listbox, zoom to and select the respective graphic in the map. privatevoid Image_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
webmapGraphicsLayer.ClearSelection();
ArcGISPortalItem portalItem = (sender as Image).DataContext as ArcGISPortalItem;
if (portalItem.Extent != null)
{
MyMap.ZoomTo(mercator.FromGeographic(portalItem.Extent));
}
// Use LINQ to select the graphic where the portal item attribute equals the portal item instance// in the Listbox.var queryPortalItemGraphics = from g in webmapGraphicsLayer.Graphics
where g.Attributes["PortalItem"] == portalItem
select g;
// Get the first graphic in the IEnumerable of graphics and set it selected.foreach (var graphic in queryPortalItemGraphics)
{
graphic.Selected = true;
return;
}
}
// Listen for when the FullExtent property changes for the graphics layer that stores web map extent center points.// When FullExtent changes (e.g. new search results are returned) zoom to an extent that contains all the results. privatevoid GraphicsLayer_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if ((e.PropertyName == "FullExtent") && ((sender as GraphicsLayer).FullExtent != null))
MyMap.ZoomTo((sender as GraphicsLayer).FullExtent);
}
}
}
Imports System.Windows
Imports System.Windows.Controls
Imports ESRI.ArcGIS.Client
Imports ESRI.ArcGIS.Client.Geometry
Imports ESRI.ArcGIS.Client.Portal
Imports System
Imports System.Windows.Documents
Imports ESRI.ArcGIS.Client.WebMap
Imports System.Linq
Namespace ArcGISWPFSDK
PartialPublicClass PortalSpatialSearch
Inherits UserControl
Private arcgisPortal As ArcGISPortal
Private mercator AsNew ESRI.ArcGIS.Client.Projection.WebMercator()
Private webmapGraphicsLayer As GraphicsLayer
PublicSubNew()
InitializeComponent()
' Search public web maps on www.arcgis.com
arcgisPortal = New ArcGISPortal() With {.Url = "http://www.arcgis.com/sharing/rest"}
webmapGraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
EndSubPrivateSub FindWebMapsButton_Click(ByVal sender AsObject, ByVal e As RoutedEventArgs)
webmapGraphicsLayer.Graphics.Clear()
' Search envelope must be in geographic (WGS84). Convert the current map extent from Web Mercator' to geographic. Dim geom As ESRI.ArcGIS.Client.Geometry.Geometry = mercator.ToGeographic(MyMap.Extent)
Dim parameters As SpatialSearchParameters = New SpatialSearchParameters With {.Limit = If(String.IsNullOrEmpty(resultLimit.Text) = True, 15, Convert.ToInt32(resultLimit.Text)), .SearchExtent = geom.Extent, .QueryString = String.Format("{0} And type:Web Map", searchText.Text)}
' Set the ItemsSource for the Listbox to an IEnumerable of ArcGISPortalItems. ' Bindings setup in the Listbox item template in XAML will enable the discovery and ' display individual result items. ' For each web map returned, add the center (point) of the web map extent as a graphic.' Add the ArcGISPortalItem instance as an attribute. This will be used to select graphics' in the map.
arcgisPortal.SearchItemsAsync(parameters, Sub(result, err)
If err IsNothingThen
WebMapsListBox.ItemsSource = result.Results
ForEach item As ArcGISPortalItem In result.Results
Dim graphic AsNew Graphic()
graphic.Attributes.Add("PortalItem", item)
Dim extentCenter As MapPoint = item.Extent.GetCenter()
graphic.Geometry = New MapPoint(extentCenter.X, extentCenter.Y, New SpatialReference(4326))
webmapGraphicsLayer.Graphics.Add(graphic)
Next item
EndIfEndSub)
EndSub' Click on a graphic in the map and select it. Scroll to the web map item in the Listbox. PrivateSub GraphicsLayer_MouseLeftButtonUp(ByVal sender AsObject, ByVal e As GraphicMouseButtonEventArgs)
webmapGraphicsLayer.ClearSelection()
Dim graphic As Graphic = e.Graphic
graphic.Selected = TrueDim portalitem As ArcGISPortalItem = TryCast(graphic.Attributes("PortalItem"), ArcGISPortalItem)
WebMapsListBox.SelectedItem = portalitem
WebMapsListBox.ScrollIntoView(portalitem)
EndSub' Click on a web map item in the Listbox, zoom to and select the respective graphic in the map. PrivateSub Image_MouseLeftButtonUp(ByVal sender AsObject, ByVal e As System.Windows.Input.MouseButtonEventArgs)
webmapGraphicsLayer.ClearSelection()
Dim portalItem As ArcGISPortalItem = TryCast((TryCast(sender, Image)).DataContext, ArcGISPortalItem)
If portalItem.Extent IsNotNothingThen
MyMap.ZoomTo(mercator.FromGeographic(portalItem.Extent))
EndIf' Use LINQ to select the graphic where the portal item attribute equals the portal item instance' in the Listbox.Dim queryPortalItemGraphics = From g As Graphic In webmapGraphicsLayer.Graphics
Where g.Attributes("PortalItem") Is portalItem
Select g
' Get the first graphic in the IEnumerable of graphics and set it selected.ForEach graphic As Graphic In queryPortalItemGraphics
graphic.Selected = TrueReturnNext graphic
EndSub' Listen for when the FullExtent property changes for the graphics layer that stores web map extent center points.' When FullExtent changes (e.g. new search results are returned) zoom to an extent that contains all the results. PrivateSub GraphicsLayer_PropertyChanged(ByVal sender AsObject, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
If (e.PropertyName = "FullExtent") AndAlso ((TryCast(sender, GraphicsLayer)).FullExtent IsNotNothing) Then
MyMap.ZoomTo((TryCast(sender, GraphicsLayer)).FullExtent)
EndIfEndSubEndClassEndNamespace