reading of my current GPS position
adding/storing a new point feature at my location
// on your save button click handler function call AsyncTask new AddFeaturesTask().execute(); // this may be placed in your activity java class body private class AddFeaturesTask extends AsyncTask<Void, Integer, Boolean> { ProgressDialog progress = null; /* (non-Javadoc) * @see android.os.AsyncTask#onCancelled() */ @Override protected void onCancelled() { super.onCancelled(); } /* (non-Javadoc) * @see android.os.AsyncTask#onPreExecute() */ @Override protected void onPreExecute() { super.onPreExecute(); // do something - show dialog for example progress = ProgressDialog.show(...); } /* (non-Javadoc) * @see android.os.AsyncTask#onPostExecute(java.lang.Object) */ @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); // hide progress dialog if it's shown if (progress != null && progress.isShowing()) { progress.dismiss(); } if (result) { // do something - alert user... } else { // do something - alert user, save data locally ...[depends on application business logic] } } @Override protected Boolean doInBackground(Void... arg0) { try { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("your layer url" + "/applyEdits"); List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); // add token, if your services are secured nameValuePairs.add(new BasicNameValuePair("token parameter here", "your token")); // json array of graphics you need to add - use your own JSON converter or String arrayAsJsonString = MyJsonHelper.fromGraphicsToJsonString(arrGraphics); nameValuePairs.add(new BasicNameValuePair("adds", arrayAsJsonString)); // add response format if you need to parse results nameValuePairs.add(new BasicNameValuePair("f", "pjson")); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity entity = response.getEntity(); String responseResult = inputStreamToString(entity.getContent()); // check response result is success to continue boolean isSaved = isSuccess(responseResult, 1); if (isSaved) { return true; } } } catch (SSLPeerUnverifiedException spuex) { Log.d(TAG, spuex.getMessage()); } catch (ClientProtocolException cpex) { Log.d(TAG, cpex.getMessage()); } catch (IOException ioex) { Log.d(TAG, ioex.getMessage()); } catch (IllegalArgumentException iaex) { Log.d(TAG, iaex.getMessage()); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } return false; } private boolean isSuccess(String responseResult, int index) { if (responseResult != null && responseResult.length() > 0) { try { JSONObject responseJSON = new JSONObject(responseResult); if (responseJSON != null) { JSONArray addResultsArray = responseJSON.getJSONArray(ADDRESULTS_TAG); // do something with results if needed // parse results and return true if sucess return true; } } catch (JSONException jsex) { Log.d(TAG, jsex.getMessage()); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } return false; } /** * Read response content */ private String inputStreamToString(InputStream is) { StringBuilder sb = new StringBuilder(); String line = ""; // Wrap a BufferedReader around the InputStream BufferedReader rd = new BufferedReader(new InputStreamReader(is)); // Read response until the end try { while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); } catch (IOException ioex) { Log.d(TAG, ioex.getMessage()); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } // Return full string return sb.toString(); } }
private static final String X_KEY = "x"; private static final String Y_KEY = "y"; private static final String GEOMETRY_KEY = "geometry"; private static final String ATTRIBUTES_KEY = "attributes"; /** * @param array of com.esri.core.map.Graphic objects * @return String representation of JSON array */ public static String fromGraphicsToJsonString(Graphic[] graphics) { if (graphics != null) { try { JSONArray arr = new JSONArray(); for (Graphic graphic : graphics) { Geometry geometry = graphic.getGeometry(); Map<String, Object> attributes = graphic.getAttributes(); if (geometry != null && geometry.getType() == Geometry.Type.Point && attributes != null) { Point pt = (Point)geometry; double xValue = pt.getX(); double yValue = pt.getY(); Map<String, Double> geometryMap = new TreeMap<String, Double>(); geometryMap.put(X_KEY, xValue); geometryMap.put(Y_KEY, yValue); JSONObject geometryObj = new JSONObject(geometryMap); JSONObject attributesObj = new JSONObject(attributes); Map<String, JSONObject> feature = new TreeMap<String, JSONObject>(); feature.put(GEOMETRY_KEY, geometryObj); feature.put(ATTRIBUTES_KEY, attributesObj); JSONObject featureObj = new JSONObject(feature); arr.put(featureObj); } else { // add empty feature JSONObject geometryObj = new JSONObject(); JSONObject attributesObj = new JSONObject(); Map<String, JSONObject> feature = new TreeMap<String, JSONObject>(); feature.put(GEOMETRY_KEY, geometryObj); feature.put(ATTRIBUTES_KEY, attributesObj); JSONObject featureObj = new JSONObject(feature); arr.put(featureObj); } } if (arr != null && arr.length() > 0) { return arr.toString(); } } catch (NullPointerException npex) { if (npex.getMessage() != null) Log.d(TAG, npex.getMessage()); else Log.d(TAG, npex.toString()); } catch (Exception ex) { Log.d(TAG, ex.getMessage()); } } return null; }
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.