0

I have a map which is loading a current address from a server. I am using on MapReady, and before its initialized it loads the first location from the server and zooms to the location. I want the app to be reading the address from the server every 10 seconds, and mapping the location. I am stuck after on MapReady on how I can remove the previous marker add the new marker from values from the server and still zoom on the location, the tutorials I have read like this haven't been so helpful. Below is my on MapReady.

 @Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
            != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(),
            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
          return;
    }
    mMap.setMyLocationEnabled(true);
    mMap.setTrafficEnabled(true);
    mMap.setIndoorEnabled(true);
    mMap.setBuildingsEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);

    LocationManager locationManager = (LocationManager) getContext().getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();

    geocoder = new Geocoder(getActivity());
    try {
        ArrayList<Address> current_adresses = (ArrayList<Address>) geocoder.getFromLocationName(current_location, 1);
        for(Address add : current_adresses){
            if (current_adresses != null) {//Controls to ensure it is right address such as country etc.
                current_longitude = add.getLongitude();
                current_latitude = add.getLatitude();
            }
        }
        ArrayList<Address> to_adresses = (ArrayList<Address>) geocoder.getFromLocationName(destination, 1);
        for(Address add : to_adresses){
            if (to_adresses != null) {//Controls to ensure it is right address such as country etc.
                to_longitude = add.getLongitude();
                to_latitude = add.getLatitude();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    location = locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, false));
    if (location != null)
    {
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(current_latitude, current_longitude))
                .title(first_name + ", " + vehicle)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.pickup)));

        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(to_latitude, to_longitude))
                .title("Destination")
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.office_building)));

        CameraPosition cameraPosition= new CameraPosition.Builder()
                .target(getCenterCoordinate())
                .zoom(13)
                .build();
        CameraUpdate camUpd3 = CameraUpdateFactory.newCameraPosition(cameraPosition);
        mMap.animateCamera(camUpd3);

        //AsyncTask to get new address
        getTransporterLocation();

        /**
         * 
         * I want to set a timer with the AsyncTask inside,
         * 
         *  remove the previous marker, convert the new address to lat and long
         *  
         *  map the marker and zoom in
         * 
         */

    }

}
Community
  • 1
  • 1
Galileo
  • 321
  • 4
  • 7

1 Answers1

0

You just need to call

mMap.clear();

and repeat your steps for drawing markers.

abdul khan
  • 843
  • 2
  • 7
  • 24