简体   繁体   中英

Google Maps Cannot Remove Marker Using Timer

I have a problem with update current markers on Google Maps . These markers are created when the activity starts. I save all merkers into a arraylist . Then I set a timer to redraw those markers every 10 seconds.

However System gives me this error:

02-26 15:33:53.295: E/AndroidRuntime(21337): java.lang.IllegalStateException: Not on the main thread

Seems like the remove function cannot be triggered because the timer is not the main thread.

How I suppose to do?

Here is the code:

public class Discover extends Activity{  

GoogleMap googleMap;
PendingIntent pendingIntent; 
SharedPreferences sharedPreferences;  
ArrayList<Marker> marker1 = new ArrayList<Marker>();  

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 

    // create timer 
    MyTimerTask myTask = new MyTimerTask(this);
    Timer myTimer = new Timer();
    myTimer.schedule(myTask, 10000, 10000);   

    setContentView(R.layout.activity_discover); 
    googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

    initialMap(this.getBaseContext()); 

    drawFriendPosition(this.getBaseContext());

}

private void drawFriendPosition(Context ctx){ 
    int i = 0;
    Cursor c = ctx.getContentResolver().query(DataProvider.CONTENT_URI_PROFILE, null, null, null, null); 
    while(c.moveToNext()){  
        Marker melbourne = googleMap.addMarker(new MarkerOptions().position( new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)))); 

        marker1.add(melbourne); 
    }  
} 
private void removeFriendPosition(){ 
    Log.i("Marker's length:", ""+marker1.size());
    Iterator<Marker> iterator = marker1.iterator();
    while (iterator.hasNext()) {
        iterator.next().remove();
    }
    iterator = marker2.iterator();
    while (iterator.hasNext()) {
        iterator.next().remove();
    } 
}  

private void initialMap(Context ctx){ 
} 

class MyTimerTask extends TimerTask {
    Discover discover = null;
    public MyTimerTask(Discover d) { 
        this.discover = d;
    }

    public void run() {  
        this.discover.removeFriendPosition();
        this.discover.drawFriendPosition(this.getBaseContext());
    }
}
}

Try this, add this code inside run() { ... } in MyTimerTask

  runOnUiThread(new Runnable(){
    @Override
    public void run() {
        this.discover.removeFriendPosition();
        this.discover.drawFriendPosition(this.getBaseContext());
    }
});

If that doesn't work I would suggest using Handler.postDelayed instead TimerTask

I changed to handler instead TimerTask It works!

Here code:

public class Discover extends Activity{  

    GoogleMap googleMap;
    PendingIntent pendingIntent; 
    SharedPreferences sharedPreferences;  
    ArrayList<Marker> marker1 = new ArrayList<Marker>();  

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 

        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() { 
                removeFriendPosition();
                drawFriendPosition();
            }
        }, 10000);

        setContentView(R.layout.activity_discover); 
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

        initialMap(this.getBaseContext()); 

        drawFriendPosition(this.getBaseContext());

    }

    private void drawFriendPosition(){ 
        int i = 0;
        Cursor c = this.getBaseContext().getContentResolver().query(DataProvider.CONTENT_URI_PROFILE, null, null, null, null); 
        while(c.moveToNext()){  
            Marker melbourne = googleMap.addMarker(new MarkerOptions().position( new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude)))); 

            marker1.add(melbourne); 
        }  
    } 
    private void removeFriendPosition(){ 
        Log.i("Marker's length:", ""+marker1.size());
        Iterator<Marker> iterator = marker1.iterator();
        while (iterator.hasNext()) {
            iterator.next().remove();
        }
        iterator = marker2.iterator();
        while (iterator.hasNext()) {
            iterator.next().remove();
        } 
    }  

    private void initialMap(Context ctx){ 
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM