@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map_view); //Set a default tap action as pop-up Map_view.setOnSingleTapListener(new OnSingleTapListener() { private static final long serialVersionUID = 1L; public void onSingleTap(float x_v, float y_v) { Point point = Map_view.toMapPoint(x_v, y_v); // Tolerance: 20 pixel Envelope env_v = new Envelope(point,20*Map_view.getResolution(), 20*Map_view.getResolution()); new setDefaultTapActions_popup(env_v, dmsl_v.getSpatialReference(), point).execute(".../MapServer/0"); } }); } //Query dynamic map service layer by QueryTask(Modified from "PopupInWebmapForViewing.java") private class setDefaultTapActions_popup extends AsyncTask<String, Void, FeatureSet> { private Envelope env_v; private SpatialReference sr; private Point point; public setDefaultTapActions_popup(Envelope env_v, SpatialReference sr, Point point) { super(); this.env_v = env_v; this.sr = sr; this.point = point; } @Override protected FeatureSet doInBackground(String... params) { //Set query parameters Query query = new Query(); query.setInSpatialReference(sr); query.setOutSpatialReference(sr); query.setGeometry(env_v); //For now, max number of returned features is 10. query.setMaxFeatures(10); query.setOutFields(new String[] { "*" }); QueryTask queryTask = new QueryTask(".../MapServer/0"); //Execute query task try { FeatureSet fs_view = queryTask.execute(query); //Get an array of graphics of the query result FeatureSet Graphic[] resultGraphic = fs_view.getGraphics(); View popupView = createPopupView(resultGraphic[0]); Map_view.getCallout().show(point, popupView); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub return null; } private View createPopupView(Graphic graphic) { //Employ "createReportView" of "MyWater" sample of ESRI LinearLayout ret = new LinearLayout(context); ret.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); Object attribute; attribute = graphic.getAttributeValue("Type_Store"); String Type_Store = null; if (attribute != null){ Type_Store = (String)attribute; } attribute = graphic.getAttributeValue("Name_Store"); String Name_Store = null; if (attribute != null){ Name_Store = (String)attribute; } attribute = graphic.getAttributeValue("Open_Close"); String Open_Close = null; if (attribute != null){ Open_Close = (String)attribute; } attribute = graphic.getAttributeValue("Service"); String Service = null; if (attribute != null){ Service = (String)attribute; } attribute = graphic.getAttributeValue("AmountSup"); Integer AmountSup = 0; if (attribute != null){ AmountSup = (Integer)attribute; } StringBuilder myPopupInfo = new StringBuilder(); myPopupInfo.append("Type of Store:\t" + Type_Store); myPopupInfo.append("\nName of Store:\t" + Name_Store); myPopupInfo.append("\nOpen or Close?:\t" + Open_Close); myPopupInfo.append("\nService:\t" + Service); myPopupInfo.append("\nAmount of Supply:\t" + AmountSup); TextView popupContent = new TextView(context); popupContent.setText(myPopupInfo.toString()); popupContent.setTextColor(Color.WHITE); popupContent.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 9); popupContent.setPadding(1, 0, 1, 0); ret.addView(popupContent); // TODO Auto-generated method stub return ret; } }
Solved! Go to Solution.
@Override protected void onPostExecute(final FeatureSet result) { //Get an array of graphics of the query result FeatureSet Graphic[] resultGraphic = result.getGraphics(); View popupView = createPopupView(resultGraphic[0]); Map_view.getCallout().show(point, popupView); }
@Override protected void onPostExecute(final FeatureSet result) { //Get an array of graphics of the query result FeatureSet Graphic[] resultGraphic = result.getGraphics(); View popupView = createPopupView(resultGraphic[0]); Map_view.getCallout().show(point, popupView); }
Try moving the lines that follow the "queryTask.execute" statement from the "doInBackground" method to the "onPostExecute" method of your AsyncTask. Hopefully the problem is limited to the issue of needing to update the GUI from only the main thread, which is performed by onPostExecute. Your sample code is missing the onPostExecute method with a FeatureSet as the incoming parameter, something like:
@Override protected void onPostExecute(final FeatureSet result) { //Get an array of graphics of the query result FeatureSet Graphic[] resultGraphic = result.getGraphics(); View popupView = createPopupView(resultGraphic[0]); Map_view.getCallout().show(point, popupView); }
I currently cannot verify what is going on in the PopupInWebmapForViewing sample as my machine is throwing errors - hopefully this will gain you some progress.
//Query dynamic map service layer by QueryTask(Modified from "PopupInWebmapForViewing.java") private class setDefaultTapActions_popup extends AsyncTask<String, Void, View> { private Envelope env_v; private SpatialReference sr; private Point point; public setDefaultTapActions_popup(Envelope env_v, SpatialReference sr, Point point) { super(); this.env_v = env_v; this.sr = sr; this.point = point; } @Override protected View doInBackground(String... params) { //Set query parameters Query query = new Query(); query.setInSpatialReference(sr); query.setOutSpatialReference(sr); query.setGeometry(env_v); //For now, max number of returned features is 10. query.setMaxFeatures(10); query.setOutFields(new String[] { "*" }); QueryTask queryTask = new QueryTask("...../MapServer/0"); //Execute query task try { FeatureSet fs_view = queryTask.execute(query); //Get an array of graphics of the query result FeatureSet Graphic[] resultGraphic = fs_view.getGraphics(); View popupView = createPopupView(resultGraphic[0]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO Auto-generated method stub return null; } @Override protected void onPostExecute(View popupView) { // TODO Auto-generated method stub super.onPostExecute(popupView); Callout callout = Map_view.getCallout(); callout.setStyle(R.xml.callout_style); callout.setContent(popupView); callout.show(point); }