1

I am having some troubles with the while loop + query. What I'm trying to do is track the user location (using onlocationChanged) and keep querying a FeatureLayer until the query returns a result and then stop the while loop is using a boolean. The problem once the "while" loop is launched is that the application freezes, here's my code:

boolean noResults=true
while(noResults)

{
    pointGeometry=MyPosition;

//myPosition is changing it's value in "onLocationChanged" using the listener, see code below 

Graphic pointGraphic = new Graphic(pointGeometry, simpleMarker);
SpatialReference spatialRef = MapFragment.mMapView.getSpatialReference();
Unit unit = spatialRef.getUnit();
double adjustedAccuracy = b_dist;
Polygon p = GeometryEngine.buffer(pointGeometry, spatialRef,adjustedAccuracy, unit);
Query q = new Query();
q.setGeometry(p);
q.setSpatialRelationship(SpatialRelationship.INTERSECTS);
fLayer.selectFeatures(q, SELECTION_METHOD.NEW, callback4);
}


public void onLocationChanged(Location loc) {

                double locy = loc.getLatitude();
            double locx = loc.getLongitude();
            Point wgspoint = new Point(locx, locy);
            mLocation = (Point) GeometryEngine.project(wgspoint,
                    SpatialReference.create(4326),
                    mMapView.getSpatialReference());

//Here's where myPosition changes its value.

            myPosition = mLocation;  
}



CallbackListener<FeatureSet> callback4 = new CallbackListener<FeatureSet>() {

        public void onCallback(FeatureSet fSet) {   
            Graphic[] grh=fSet.getGraphics();

            if(grh.length>0){  
                        noResults=false;
                        showTaost("there's a result");
                    } 
});

Is there an alternative to the while loop to fix this? If not, how can I use something like a delay (query the Feature Layer every 5 secondes or something like that)?

0

I resolved the problem using a "Handler" that query the Feature Layer every 5 seconds, this stops the main thread from generating application not responding error:

   Handler m_handler=new Handler();
    Runnable m_runnable;
    m_runnable = new Runnable(){
   public void run() {

  //query code here
m_handler.postDelayed(m_runnable, 5000);

    }
   };
  m_handler.postDelayed(m_runnable, 0);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.