com.esri.core.io.EsriSecurityException: EsriSecurityException[code = -10001 msg = User credentials were invalid. Please supply a valid username and password in order to access the secure service. at com.esri.core.internal.io.handler.c.a(Unknown Source) at com.esri.core.internal.io.handler.r.a(Unknown Source) at com.esri.core.internal.io.handler.r.a(Unknown Source) at com.esri.core.internal.tasks.d.getServiceCredentials(Unknown Source) at com.esri.core.internal.tasks.ags.a.a.a(Unknown Source) at com.esri.core.tasks.ags.find.FindTask.execute(Unknown Source) at project01.FindApp.onFind(FindApp.java:119) at project01.FindApp.access$1(FindApp.java:94) at project01.FindApp$2.actionPerformed(FindApp.java:203) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)
Solved! Go to Solution.
// Copyright 2013 ESRI /*** * This sample demonstrates executing a FindTask, adding the results to * a table, and zooming to a selected result. */ public class FindApp { // UI related JTextField findText = new JTextField("Colorado"); DefaultTableModel tblModelStateInfo; // JMap private JMap map; // our graphics layer for highlighted geometries private GraphicsLayer graphicsLayer; // for zooming to a buffered geometry private static final double BUFFER_DISTANCE = 500000; // 500 km // results of the Find task execution List<FindResult> findResults = null; // ------------------------------------------------------------------------ // Constructors // ------------------------------------------------------------------------ public FindApp() { } // ------------------------------------------------------------------------ // Core functionality // ------------------------------------------------------------------------ /** * This method creates and executes the FindTask to find the input text * */ private void onFind() { tblModelStateInfo.setRowCount(0); // return if input text is not valid if (findText.getText() == null || findText.getText().isEmpty()) { return; } // ----------------------------------------------------------------------------------------- // Find task to find value 'Colorado' in all attributes of dynamic // service // ----------------------------------------------------------------------------------------- // create a Find task. FindParameters params = new FindParameters(); params.setSearchText(findText.getText()); params.setLayerIds(new int[] { 0, 1, 2,3,4,5,6,7,8}); params.setSearchFields(new String[]{"Streets"});//search only the named field params.setOutputSpatialRef(map.getSpatialReference()); // execute the query FindTask task = new FindTask("C:\\mpk.mpk"); try { findResults = task.execute(params); if (findResults == null){ return; } for (FindResult result : findResults) { tblModelStateInfo.addRow(new Object[] { result.getAttributes().get(result.getDisplayFieldName()), Integer.valueOf(result.getLayerId()), result.getLayerName(), result.getFoundFieldName(), result.getValue() }); } } catch (Exception e) { e.printStackTrace(); } } // ------------------------------------------------------------------------ // Private methods // ------------------------------------------------------------------------ /** * Creates a window. * * @return a window. */ private JFrame createWindow() { JFrame window = new JFrame("Find Application"); window.setBounds(100, 100, 1000, 700); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.getContentPane().setLayout(new BorderLayout(0, 0)); window.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent windowEvent) { super.windowClosing(windowEvent); map.dispose(); } }); return window; } /** * Creates a map. * * @return a map. */ private JMap createMap() throws Exception { final JMap jMap = new JMap(); // ----------------------------------------------------------------------------------------- // Base Layer - with US topology, focus on U.S by default // ----------------------------------------------------------------------------------------- final ArcGISLocalTiledLayer baseLayer = new ArcGISLocalTiledLayer( "C:\\tpk.tpk"); //local tpk added here jMap.setExtent(new Envelope(-15000000, 2000000, -7000000, 8000000)); jMap.getLayers().add(baseLayer); // ----------------------------------------------------------------------------------------- // Dynamic Layer - US Cities and States that we will search // ----------------------------------------------------------------------------------------- final ArcGISLocalDynamicMapServiceLayer layer = new ArcGISLocalDynamicMapServiceLayer( "C:\\mpk.mpk"); // I added the local mpk here layer.setOpacity(0.6f); jMap.getLayers().add(layer); // ----------------------------------------------------------------------------------------- // Graphics Layer - to highlight a selected feature // ----------------------------------------------------------------------------------------- graphicsLayer = new GraphicsLayer(); jMap.getLayers().add(graphicsLayer); return jMap; } /** * Display a geometry on the map for the selected table row * * @param geometryType * @param geometry */ private void highlightGeometry(Type geometryType, Geometry geometry) { if (geometryType == null || geometry == null) { return; } // ----------------------------------------------------------------------------------------- // Add a graphic for the selected feature to the graphics layer // ----------------------------------------------------------------------------------------- graphicsLayer.removeAll(); switch (geometryType) { case POINT: graphicsLayer.addGraphic(new Graphic(geometry, new SimpleMarkerSymbol(Color.RED, 8, SimpleMarkerSymbol.Style.DIAMOND))); break; case POLYLINE: graphicsLayer.addGraphic(new Graphic(geometry, new SimpleLineSymbol(Color.RED, 4, SimpleLineSymbol.Style.SOLID))); break; case POLYGON: graphicsLayer.addGraphic(new Graphic(geometry, new SimpleFillSymbol(Color.RED))); break; default: break; } // ----------------------------------------------------------------------------------------- // Zoom to the highlighted graphic // ----------------------------------------------------------------------------------------- Geometry geometryForZoom = GeometryEngine.buffer(geometry, map.getSpatialReference(), BUFFER_DISTANCE, map .getSpatialReference().getUnit()); map.zoomTo(geometryForZoom); } }
You can't run the FindTask against a path to your mpk - FindTask task = new FindTask("C:\\mpk.mpk") - you need to supply a URL.
To get the URL you will need to create a local layer from your MPK and then use the URL of that in the constructor. If you look at the samples in com.esri.client.samples.localserver you will see how to create local layers. You can then use getURL() on the local layer to get a URL for your find task.
You would need to do...
LocalMapService locMapService = new LocalMapService(Path to mpk);
locMapService.start(); // start the local service
String url = locMapService.getUrlMapService(); // this is the URL of the maps service
// to do find task you need url to a layer
url += "/0"; // add the ID of a layer
FindTask findTask = new FindTask(url); // set up your find task
If you look in the ConsoleApp sample (com.esri.client.samples.localserver) you will see this work flow.
A simpler work flow for you could be to use ArcGISLocalFeatureLayer which will create a local server based feature layer and if you call getUrl() that url will be the complete URL including layer ID. LocalMapService and LocalFeatureService are only really intended for when you want to create local services with multiple layers. ArcGISLocalFeatureLayer can be added directly to the map which will take care of any initialisation for you.