-1

I have a timer.it is running every 5 minute.ı write a method called konumlarıAl in timer run method. this method get locations data from database.When konumlarıAl run,HaritaKonumGoster is method calling.I want to delete all markers and show new location data marker on map without refreshing page.

my code

private void HaritaKonumGoster() {
        // TODO Auto-generated method stub
        if (googleHarita == null) {
            googleHarita = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.haritafragment))
                    .getMap();
            if (googleHarita != null) {
                googleHarita.clear();
                if(mrks.size()!=0)
                {
                     for (Marker marker: mrks) {
                            marker.remove();
                        }
                        mrks.clear();
                }
                googleHarita.setMyLocationEnabled(true);

                LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);

                Criteria criteria=new Criteria();

                String provider =locationManager.getBestProvider(criteria, true);

                Location mylocation=locationManager.getLastKnownLocation(provider);
                 double latitude=0;
                double longitude=0;
                double mylatitude=0;
                double myLongtitude=0;
                 //double latitude=enlem;
                    //double longitude=boylam;
                if (mylocation != null){
                    mylatitude=mylocation.getLatitude();
                    myLongtitude=mylocation.getLongitude();
                }
                BitmapDescriptor bitmapDescriptor 
                   = BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN);

                try{  
              for (int i = 0; i < degiskenler.taksici.size(); i++) {

                   latitude=Double.parseDouble(degiskenler.taksici.get(i).enlem.trim());
                   longitude=Double.parseDouble(degiskenler.taksici.get(i).boylam.trim());
                   LatLng istanbulKoordinat = new LatLng(latitude,longitude);
                   Marker m= googleHarita.addMarker(new MarkerOptions().position(istanbulKoordinat).title("Kız Kulesi").icon(bitmapDescriptor));
                   googleHarita.moveCamera(CameraUpdateFactory.newLatLngZoom(istanbulKoordinat, 7)); 
                   mrks.add(m);
               //   Toast.makeText(getBaseContext(),"harita :)",Toast.LENGTH_LONG).show();

               }
                }
                catch(Exception exception)  {

                    Toast.makeText(getBaseContext(),"harita olmadı",Toast.LENGTH_LONG).show();
                }

                googleHarita.addMarker(new MarkerOptions().position(new LatLng(mylatitude, myLongtitude)).title("you hereeee"));
            }
        }
    }

location update every 5 minute method

  private void LocationUpdateEvery5minute() {
        // TODO Auto-generated method stub

               zamanlayici = new Timer();
               yardimci = new Handler(Looper.getMainLooper());
               zamanlayici.scheduleAtFixedRate(new TimerTask()
               {
           @Override
           public void run(){ 
                yardimci.post(new Runnable()
               {
                 public void run()
                 {  Toast.makeText(getBaseContext(), "timera girdi",Toast.LENGTH_SHORT).show();
                     konumlarıAl();

         }
        });    

       }

      }, 0, ZAMAN);


            }
duggu
  • 37,851
  • 12
  • 116
  • 113
user3452425
  • 73
  • 2
  • 10

2 Answers2

0

Delete markers using

 googleHarita.clear()

https://developer.android.com/reference/com/google/android/gms/maps/GoogleMap.html#clear()

Just one call to this method should be enough to remove all the markers on your map.

Then, and add markers using

googleHarita.addMarker()

http://developer.android.com/reference/com/google/android/gms/maps/model/Marker.html

example here

I don't think you need to refresh any pages.

yildirimyigit
  • 3,013
  • 4
  • 25
  • 35
  • googleHarita.clear() method didn't run.When map is refreshing,new markers added with old markers.I did not delete old markers when map is refreshing – user3452425 Jun 02 '14 at 12:30
  • Do you get an exception when Timer runs? And also please show a Toast after if(googleHarita != null) to see whether it runs within your Timer object. I believe your map instance is null when timer calls it. Therefore if block is not executed. – yildirimyigit Jun 02 '14 at 12:43
  • @user3452425 could you please try it out and give us a feedback? – yildirimyigit Jun 02 '14 at 12:53
0

You have to use map.clear(); which will help you to removes all markers, polylines, polygons, overlays, etc from the map.

Mahendran Candy
  • 1,114
  • 17
  • 17