Hi everyone,
I'm using the Access feature data in the cloud sample with my own service from ArcGis online, i have no particular error in my code and my basemap appear in the app. Can't find why my feature layer "monuments" doesn't appear. Any ideas? Thanks!
MainActivity
package com.arcgis.regeul_s;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.TextView;
import com.esri.android.map.Callout;
import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.Layer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.Point;
import com.esri.core.map.Feature;
import com.esri.core.map.FeatureResult;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.tasks.query.QueryParameters;
import com.esri.core.tasks.query.QueryTask;
public class REGEUL_SActivity extends Activity {
MapView mMapView;
private ArcGISFeatureLayer mFeatureLayer;
GraphicsLayer mGraphicsLayer;
private Callout mCallout;
private Graphic mIdentifiedGraphic;
private int mCalloutStyle;
private ViewGroup mCalloutContent;
boolean mIsMapLoaded;
private String mFeatureServiceURL;
ProgressDialog progress;
// The query params switching menu items.
MenuItem mQueryMdMenuItem = null;
MenuItem mQueryPkMenuItem = null;
MenuItem mQueryAuMenuItem = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Retrieve the map and initial extent from XML layout
mMapView = (MapView) findViewById(R.id.map);
// Get the feature service URL from values->strings.xml
mFeatureServiceURL = this.getResources().getString(R.string.featureServiceURL);
// Add Feature layer to the MapView
mFeatureLayer = new ArcGISFeatureLayer(mFeatureServiceURL, ArcGISFeatureLayer.MODE.ONDEMAND);
mMapView.addLayer(mFeatureLayer);
// Add Graphics layer to the MapView
mGraphicsLayer = new GraphicsLayer();
mMapView.addLayer(mGraphicsLayer);
// Set the Esri logo to be visible, and enable map to wrap around date line.
mMapView.setEsriLogoVisible(true);
mMapView.enableWrapAround(true);
// Get the MapView's callout from xml->identify_calloutstyle.xml
mCalloutStyle = R.xml.identify_calloutstyle;
LayoutInflater inflater = getLayoutInflater();
mCallout = mMapView.getCallout();
// Get the layout for the Callout from
// layout->identify_callout_content.xml
mCalloutContent = (ViewGroup) inflater.inflate(R.layout.identify_callout_content, null);
mCallout.setContent(mCalloutContent);
mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
private static final long serialVersionUID = 1L;
public void onStatusChanged(Object source, STATUS status) {
// Check to see if map has successfully loaded
if ((source == mMapView) && (status == STATUS.INITIALIZED)) {
// Set the flag to true
mIsMapLoaded = true;
}
}
});
mMapView.setOnSingleTapListener(new OnSingleTapListener() {
private static final long serialVersionUID = 1L;
public void onSingleTap(float x, float y) {
if (mIsMapLoaded) {
// If map is initialized and Single tap is registered on screen
// identify the location selected
identifyLocation(x, y);
}
}
});
}
/**
* Takes in the screen location of the point to identify the feature on map.
*
* @param x
* x co-ordinate of point
* @param y
* y co-ordinate of point
*/
void identifyLocation(float x, float y) {
// Hide the callout, if the callout from previous tap is still showing
// on map
if (mCallout.isShowing()) {
mCallout.hide();
}
// Find out if the user tapped on a feature
SearchForFeature(x, y);
// If the user tapped on a feature, then display information regarding
// the feature in the callout
if (mIdentifiedGraphic != null) {
Point mapPoint = mMapView.toMapPoint(x, y);
// Show Callout
ShowCallout(mCallout, mIdentifiedGraphic, mapPoint);
}
}
/**
* Sets the value of mIdentifiedGraphic to the Graphic present on the
* location of screen tap
*
* @param x
* x co-ordinate of point
* @param y
* y co-ordinate of point
*/
private void SearchForFeature(float x, float y) {
Point mapPoint = mMapView.toMapPoint(x, y);
if (mapPoint != null) {
for (Layer layer : mMapView.getLayers()) {
if (layer == null)
continue;
if (layer instanceof ArcGISFeatureLayer) {
ArcGISFeatureLayer fLayer = (ArcGISFeatureLayer) layer;
// Get the Graphic at location x,y
mIdentifiedGraphic = GetFeature(fLayer, x, y);
}
}
}
}
/**
* Returns the Graphic present the location of screen tap
*
* @param fLayer
* ArcGISFeatureLayer to get graphics ids
* @param x
* x co-ordinate of point
* @param y
* y co-ordinate of point
* @return Graphic at location x,y
*/
private Graphic GetFeature(ArcGISFeatureLayer fLayer, float x, float y) {
// Get the graphics near the Point.
int[] ids = fLayer.getGraphicIDs(x, y, 10, 1);
if (ids == null || ids.length == 0) {
return null;
}
return fLayer.getGraphic(ids[0]);
}
/**
* Shows the Attribute values for the Graphic in the Callout
*
* @param calloutView a callout to show
* @param graphic selected graphic
* @param mapPoint point to show callout on map
*/
private void ShowCallout(Callout calloutView, Graphic graphic, Point mapPoint) {
// Get the values of attributes for the Graphic
String matricule = (String) graphic.getAttributeValue("Matricule").toString();
String mater = (String) graphic.getAttributeValue("Materialisation").toString();
String latitude = graphic.getAttributeValue("Latitude").toString();
String longitude = graphic.getAttributeValue("Longitude").toString();
String altitude = graphic.getAttributeValue("Altitude").toString();
String x_utm19 = graphic.getAttributeValue("X_UTM19").toString();
String y_utm19 = graphic.getAttributeValue("Y_UTM19").toString();
String x_mtm7 = graphic.getAttributeValue("X_MTM7").toString();
String y_mtm7 = graphic.getAttributeValue("Y_MTM7").toString();
String hauteur = graphic.getAttributeValue("Hauteur").toString();
String ondul = graphic.getAttributeValue("Ondulation").toString();
String xi = graphic.getAttributeValue("Xi").toString();
String eta = graphic.getAttributeValue("Eta").toString();
String prec_n = graphic.getAttributeValue("Prec_N").toString();
String prec_e = graphic.getAttributeValue("Prec_E").toString();
String prec_v = graphic.getAttributeValue("Prec_V").toString();
String tech_mes = graphic.getAttributeValue("Tech_mesure").toString();
String comment = graphic.getAttributeValue("Commentaire").toString();
// Set callout properties
calloutView.setCoordinates(mapPoint);
calloutView.setStyle(mCalloutStyle);
calloutView.setMaxWidth(325);
// Compose the string to display the results
StringBuilder pointMatriculeName = new StringBuilder();
pointMatriculeName.append("Matricule: ");
pointMatriculeName.append(matricule);
TextView calloutTextLine1 = (TextView) findViewById(R.id.Matricule);
calloutTextLine1.setText(pointMatriculeName);
// Compose the string to display the results
StringBuilder pointMaterName = new StringBuilder();
pointMaterName.append("Matérisalisation: ");
pointMaterName.append(mater);
TextView calloutTextLine2 = (TextView) findViewById(R.id.Mater);
calloutTextLine2.setText(pointMaterName);
// Compose the string to display the results
StringBuilder pointLatitudeName = new StringBuilder();
pointLatitudeName.append("Latitude: ");
pointLatitudeName.append(latitude);
TextView calloutTextLine3 = (TextView) findViewById(R.id.Latitude);
calloutTextLine3.setText(pointLatitudeName);
// Compose the string to display the results
StringBuilder pointLongitudeName = new StringBuilder();
pointLongitudeName.append("Longitude: ");
pointLongitudeName.append(longitude);
TextView calloutTextLine4 = (TextView) findViewById(R.id.Longitude);
calloutTextLine4.setText(pointLongitudeName);
// Compose the string to display the results
StringBuilder pointAltitudeName = new StringBuilder();
pointAltitudeName.append("Altitude: ");
pointAltitudeName.append(altitude);
TextView calloutTextLine5 = (TextView) findViewById(R.id.Altitude);
calloutTextLine5.setText(pointAltitudeName);
// Compose the string to display the results
StringBuilder pointX_UTM19Name = new StringBuilder();
pointX_UTM19Name.append("X_UTM19: ");
pointX_UTM19Name.append(x_utm19);
TextView calloutTextLine6 = (TextView) findViewById(R.id.X_UTM19);
calloutTextLine6.setText(pointX_UTM19Name);
// Compose the string to display the results
StringBuilder pointY_UTM19Name = new StringBuilder();
pointY_UTM19Name.append("Y_UTM19: ");
pointY_UTM19Name.append(y_utm19);
TextView calloutTextLine7 = (TextView) findViewById(R.id.Y_UTM19);
calloutTextLine7.setText(pointY_UTM19Name);
// Compose the string to display the results
StringBuilder pointX_MTM7Name = new StringBuilder();
pointX_MTM7Name.append("X_MTM7: ");
pointX_MTM7Name.append(x_mtm7);
TextView calloutTextLine8 = (TextView) findViewById(R.id.X_MTM7);
calloutTextLine8.setText(pointX_MTM7Name);
// Compose the string to display the results
StringBuilder pointY_MTM7Name = new StringBuilder();
pointY_MTM7Name.append("Y_MTM7: ");
pointY_MTM7Name.append(y_mtm7);
TextView calloutTextLine9 = (TextView) findViewById(R.id.Y_MTM7);
calloutTextLine9.setText(pointY_MTM7Name);
// Compose the string to display the results
StringBuilder pointHauteurName = new StringBuilder();
pointHauteurName.append("Hauteur: ");
pointHauteurName.append(hauteur);
TextView calloutTextLine10 = (TextView) findViewById(R.id.Hauteur);
calloutTextLine10.setText(pointHauteurName);
// Compose the string to display the results
StringBuilder pointOndulationName = new StringBuilder();
pointOndulationName.append("Ondulation: ");
pointOndulationName.append(ondul);
TextView calloutTextLine11 = (TextView) findViewById(R.id.Ondulation);
calloutTextLine11.setText(pointOndulationName);
// Compose the string to display the results
StringBuilder pointXiName = new StringBuilder();
pointXiName.append("Xi: ");
pointXiName.append(xi);
TextView calloutTextLine12 = (TextView) findViewById(R.id.Xi);
calloutTextLine12.setText(pointXiName);
// Compose the string to display the results
StringBuilder pointEtaName = new StringBuilder();
pointEtaName.append("Eta: ");
pointEtaName.append(eta);
TextView calloutTextLine13 = (TextView) findViewById(R.id.Eta);
calloutTextLine13.setText(pointEtaName);
// Compose the string to display the results
StringBuilder pointPrec_NName = new StringBuilder();
pointPrec_NName.append("Prec_N: ");
pointPrec_NName.append(prec_n);
TextView calloutTextLine14 = (TextView) findViewById(R.id.Prec_N);
calloutTextLine14.setText(pointPrec_NName);
// Compose the string to display the results
StringBuilder pointPrec_EName = new StringBuilder();
pointPrec_EName.append("Prec_E: ");
pointPrec_EName.append(prec_e);
TextView calloutTextLine15 = (TextView) findViewById(R.id.Prec_E);
calloutTextLine15.setText(pointPrec_EName);
// Compose the string to display the results
StringBuilder pointPrec_VName = new StringBuilder();
pointPrec_VName.append("Prec_V: ");
pointPrec_VName.append(prec_v);
TextView calloutTextLine16 = (TextView) findViewById(R.id.Prec_V);
calloutTextLine16.setText(pointPrec_VName);
// Compose the string to display the results
StringBuilder pointTech_mesName = new StringBuilder();
pointTech_mesName.append("tech_mes: ");
pointTech_mesName.append(tech_mes);
TextView calloutTextLine17 = (TextView) findViewById(R.id.Tech_mes);
calloutTextLine17.setText(pointTech_mesName);
// Compose the string to display the results
StringBuilder pointCommentName = new StringBuilder();
pointCommentName.append("Comment: ");
pointCommentName.append(comment);
TextView calloutTextLine18 = (TextView) findViewById(R.id.Comment);
calloutTextLine18.setText(pointCommentName);
calloutView.setContent(mCalloutContent);
calloutView.show();
}
/**
* Run the query task on the feature layer and put the result on the map.
*/
private class QueryFeatureLayer extends AsyncTask<String, Void, FeatureResult> {
// default constructor
public QueryFeatureLayer() {
}
@Override
protected void onPreExecute() {
progress = ProgressDialog.show(REGEUL_SActivity.this, "", "Please wait....query task is executing");
}
@Override
protected FeatureResult doInBackground(String... params) {
String whereClause = "COUNTRY='" + params[0] + "'";
// Define a new query and set parameters
QueryParameters mParams = new QueryParameters();
mParams.setWhere(whereClause);
mParams.setReturnGeometry(true);
// Define the new instance of QueryTask
QueryTask queryTask = new QueryTask(mFeatureServiceURL);
FeatureResult results;
try {
// run the querytask
results = queryTask.execute(mParams);
return results;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(FeatureResult results) {
// Remove the result from previously run query task
mGraphicsLayer.removeAll();
// Define a new marker symbol for the result graphics
SimpleMarkerSymbol sms = new SimpleMarkerSymbol(Color.BLUE, 10, SimpleMarkerSymbol.STYLE.CIRCLE);
// Envelope to focus on the map extent on the results
Envelope extent = new Envelope();
// iterate through results
for (Object element : results) {
// if object is feature cast to feature
if (element instanceof Feature) {
Feature feature = (Feature) element;
// convert feature to graphic
Graphic graphic = new Graphic(feature.getGeometry(), sms, feature.getAttributes());
// merge extent with point
extent.merge((Point)graphic.getGeometry());
// add it to the layer
mGraphicsLayer.addGraphic(graphic);
}
}
// Set the map extent to the envelope containing the result graphics
mMapView.setExtent(extent, 100);
// Disable the progress dialog
progress.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
// Get the query params menu items.
mQueryMdMenuItem = menu.getItem(0);
mQueryPkMenuItem = menu.getItem(1);
mQueryAuMenuItem = menu.getItem(2);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle menu item selection.
switch (item.getItemId()) {
case R.id.Query_MD:
mQueryMdMenuItem.setChecked(true);
new QueryFeatureLayer().execute("Medaillon");
return true;
case R.id.Query_PK:
mQueryPkMenuItem.setChecked(true);
new QueryFeatureLayer().execute("Pk");
return true;
case R.id.Query_AU:
mQueryAuMenuItem.setChecked(true);
new QueryFeatureLayer().execute("Autre");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onPause() {
super.onPause();
mMapView.pause();
}
@Override
protected void onResume() {
super.onResume();
mMapView.unpause();
}
}
Strings
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">REGEUL_S</string>
<string name="spinTitle">Query Customer Data:</string>
<string name="featureServiceURL">http://http://services3.arcgis.com/D7xw8Tli8HKiA30u/ArcGIS/rest/services/monuments/FeatureServer/0</string>
<!-- query parameters -->
<string name="Query_md">Medaillon</string>
<string name="Query_pk">PK</string>
<string name="Query_au">Autre</string>
</resources>