Creating a local geometry service for use with editor control

2390
5
11-17-2011 01:15 PM
Labels (1)
ArtUllman
New Contributor
I found an examples shows how to edit polygons using the Editor class in online mode.  I need the same functionality in disconnected mode.  Since I want to use the autocomplete and other functionality, I need to use a geometry service with the editor.

Here is the samplecode in XAML for connected mode:

<esri:Editor x:Key="MyEditor"                         
                         LayerIDs="MyLayer"  
GeometryServiceUrl="http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"
                             />

I need to replace the remote geometry service with a local service for offline mode. 

I am not sure how to include a local geometry service in XAML, and I keep getting errors trying to create, start and reference the URL in code.  Whe I run the following code, it keeps telling me that local services are starting, so I can't start my localService.  Here the code, which I believe is correct, but I can't figure out when I can run it since I don't know when local service startup has completed.

LocalGeometryService  _localGeometryService = new LocalGeometryService();
_localGeometryService.Start();
ESRI.ArcGIS.Client.Editor myEditor = LayoutRoot.Resources["MyEditor"] as ESRI.ArcGIS.Client.Editor;
myEditor.GeometryServiceUrl = _localGeometryService.Url;

My question is:
1. How do I add a local geometry service to my Editor reference in XAML?
2. If I can't do it xaml, how do I do it in code?  Is there a point in the life cycle that I can create/start/reference the local geometry service without throwing an exception?

Thanks.
0 Kudos
5 Replies
MichaelBranscomb
Esri Frequent Contributor
Hi,

To do this in code you can register a handler for the EditorWidget Loaded event then in code within the handler, set the GeometryServiceUrl property on the EditorWidget. In the code below a class member has been declared for the Local Geometry Service which is started in the default constructor (before the InitializeComponent call). Alternatively you could start the LocalGeometryService in the handler.

XAML:

<esri:EditorWidget x:Name="_EditorWidget"
Map="{Binding ElementName=MapControl}"
Width="300"
AutoSelect="False"
ShowAttributesOnAdd="True"
Loaded="_EditorWidget_Loaded"/>

CODE:

// Class member for LocalGeometryService 
LocalGeometryService _LocalGeometryService = new LocalGeometryService();

// Default constructor
public MainWindow()
{
_LocalGeometryService.Start();
InitializeComponent();
...
... //Other code e.g. for spinning up a LocalFeatureService
...
}

// EditorWidget Loaded handler
private void _EditorWidget_Loaded(object sender, RoutedEventArgs e)
{
_EditorWidget.GeometryServiceUrl = _LocalGeometryService.UrlGeometryService;
}


Cheers

Mike
0 Kudos
ArtUllman
New Contributor
Hi Mike,

I am using the Editor Class rather than the editorwidget, so I don't have an event for Loaded.

I define the class as resource in the XAML.

<Grid.Resources>
<esri:Editor x:Key="MyEditor"                         
                         LayerIDs="PLU"   Freehand="False" AutoComplete="True" AutoSelect="True"
                         EditCompleted="edit_completed" ContinuousMode="True" ScaleEnabled="False" RotateEnabled="False"
                        GeometryServiceUrl="http://tasks.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer"
                             />
</Grid.resources>

I am using the coding example from:

http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#EditToolsExplicitSave

Any thoughts on how to define a local geometry service in this scenario?

Thanks!
0 Kudos
RobertZargarian
New Contributor III
Hi,
I have the same problem. Is this right approach?


in xaml
<esri:Editor x:Key="MyEditor" LayerIDs="ThreatAreas"   />
..
<esri:Map x:Name="MyMap" Extent="245118,6111275,1884296,7671051" Loaded="MyMap_Loaded" WrapAround="True">
            <esri:ArcGISLocalFeatureLayer ID="ThreatAreas" Path="C:\\...\map.mpk" 
                                          LayerName="ThreatAreas"
                                          Editable="True"
                                          AutoSave="False"
                                          OutFields="*"
                                          Mode="OnDemand"
                                          DisableClientCaching="True" />

CODE:

         private void MyMap_Loaded(object sender, RoutedEventArgs e)
        {
          
            LocalGeocodeService geoServ = new LocalGeocodeService("C:\\Data\\GISS\\Geodatabaser\\test\\ch_test2.mpk");
            geoServ.StartCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(geoServ_StartCompleted);
            //geoServ.StartAsync();
            geoServ.StartAsync(sl =>
            {
                geoServ.Name = "test";
                ....
            });
           
        }

        void geoServ_StartCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("Failed " + e.Error);
                return;
            }
            ESRI.ArcGIS.Client.Editor myEditor = LayoutRoot.Resources["MyEditor"] as ESRI.ArcGIS.Client.Editor;
            myEditor.Map = MyMap;
        }


I am geting ERROR:

{"Failed to create service ch_test22 GeocodeServer. Service failed to initialize: Error opening locator Error code: 500"}
    [ESRI.ArcGIS.Client.Local.LocalServerException]: {"Failed to create service ch_test22 GeocodeServer. Service failed to initialize: Error opening locator Error code: 500"}
    Data: {System.Collections.ListDictionaryInternal}
    HelpLink: null
    InnerException: null
    Message: "Failed to create service ch_test22 GeocodeServer. Service failed to initialize: Error opening locator Error code: 500"
    Source: null
    StackTrace: null
    TargetSite: null
0 Kudos
MichaelBranscomb
Esri Frequent Contributor
Hi,

There is just a minor issue in your approach here - which are most likely the cause of the problem you're experiencing.

You're trying to start a LocalGeocodeService rather than a LocalGeometryService. Another thing to note, for when you do start working with geocode services, is that you can't start a local geocode service from a map package (MPK) - it needs to be an address locator package (APK).

So your original code...

            LocalGeocodeService geoServ = new LocalGeocodeService("C:\\Data\\GISS\\Geodatabaser\\test\\ch_test2.mpk");

...Would work better like this (within the default constructor of your application):

            LocalGeometryService localGeometryService = new LocalGeometryService();
            localGeometryService.StartCompleted += (sender, eventargs) =>
            {
                ESRI.ArcGIS.Client.Editor myEditor = LayoutRoot.Resources["MyEditor"] as ESRI.ArcGIS.Client.Editor;
                myEditor.GeometryServiceUrl = localGeometryService.UrlGeometryService;  
                // Enable UI as required
                // e.g. AddButton.IsEnabled = true;
            };
            localGeometryService.StartAsync();   


Cheers

Mike
0 Kudos
RobertZargarian
New Contributor III
Thanks!
Now it works fine.

private void MyMap_Loaded(object sender, RoutedEventArgs e)
{
          
            LocalGeometryService geoServ = new LocalGeometryService();
            geoServ.StartCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(geoServ_StartCompleted);
           geoServ.StartAsync();
          
}

void geoServ_StartCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
            ESRI.ArcGIS.Client.Editor myEditor = LayoutRoot.Resources["MyEditor"] as ESRI.ArcGIS.Client.Editor;
            myEditor.Map = MyMap;
            myEditor.GeometryServiceUrl = ((LocalGeometryService)sender).UrlGeometryService;
}
0 Kudos