Question

I am using Google map v2 in my app. i am placing the marker where i click. when i click again on map the privios marke gone and new marker added. when i check the address then it shows me the address of removed marker. i.e it contains the the last touched location. Please help me. How can i remove address as well as marker removed. Here is my code

public class Map extends FragmentActivity {
GoogleMap gMap;
static int loginCheck = 0;
GeoPoint p, currentLocationPixels;
ConnectionDetector conDec;
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
SharedPreferences prefs;
LatLng latLng;
MarkerOptions markerOptions;
EditText desc;
String addressText = "";

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

    setContentView(R.layout.map);

    prefs = getApplicationContext().getSharedPreferences("jam",
            MODE_PRIVATE);

    conDec = new ConnectionDetector(this);

    SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapView);
    gMap = smf.getMap();

    gMap.setMyLocationEnabled(true);
    gMap.getUiSettings().setCompassEnabled(true);
    // gMap.setTrafficEnabled(true);


    gMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {

            // Getting the Latitude and Longitude of the touched location
            latLng = arg0;

            // Clears the previously touched position
            gMap.clear();
            addressText=null;

            // Animating to the touched position
            // gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

            int caller = getIntent().getIntExtra("button", 0);
            System.out.println(caller);
            switch (caller) {
            case R.id.btMap:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_darkblue)).title(addressText));
                break;
            case R.id.imageButton1:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_bue)).title(addressText));
                break;
            case R.id.imageButton2:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_green)).title(addressText));
                break;
            case R.id.imageButton3:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_light)).title(addressText));
                break;
            case R.id.imageButton4:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_purple)).title(addressText));
                break;
            case R.id.imageButton5:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_red)).title(addressText));
                break;
            case R.id.imageButton6:
                gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_yellow)).title(addressText));
                break;
            }

            // Creating a marker
            // markerOptions = new MarkerOptions();

            // Setting the position for the marker
            // markerOptions.position(latLng);

            // Placing a marker on the touched position
            // gMap.addMarker(markerOptions);

            // Adding Marker on the touched location with address
            new ReverseGeocodingTask(getBaseContext()).execute(latLng);
        }
    });

    // ** Hide By Gaurav
    // gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
    // @Override
    // public void onInfoWindowClick(Marker marker) {
    // System.out.println(marker.getPosition().latitude);
    //
    // Intent i = new Intent(Map.this, Detail.class);
    // i.putExtra("lat", "" + marker.getPosition().latitude);
    // i.putExtra("lng", "" + marker.getPosition().longitude);
    // i.putExtra("title", marker.getTitle());
    // i.putExtra("desc", marker.getSnippet());
    // startActivity(i);
    // }
    // });
    getInfo();

    CameraPosition cp = new CameraPosition.Builder()
            .target(new LatLng(Double.parseDouble("30.7353"), Double
                    .parseDouble("76.7911"))).zoom(16).build();
    gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}

// ** Gaurav Work Start Here
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
    Context mContext;

    public ReverseGeocodingTask(Context context) {
        super();
        mContext = context;
    }

    // Finding address using reverse geocoding
    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(mContext);
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;
        List<Address> addresses = null;
        //String addressText = "";
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);

            addressText = String.format(
                    "%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "", address.getLocality(),
                    address.getCountryName());
        }

        return addressText;
    }


    // @Override
    // protected void onPostExecute(String addressText) {
    //
    // // This will be displayed on taping the marker
    // markerOptions.title(addressText);
    //
    // // Placing a marker on the touched position
    // gMap.addMarker(markerOptions);
    //
    // }
}}
Was it helpful?

Solution

I have solved this with. its my own mistake. it will help you.

public class Map extends FragmentActivity {
GoogleMap gMap;
static int loginCheck = 0;
GeoPoint p, currentLocationPixels;
ConnectionDetector conDec;
ArrayList<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
SharedPreferences prefs;
LatLng latLng;
MarkerOptions markerOptions;
EditText desc;
String selectedLocAddress;

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

    setContentView(R.layout.map);

    prefs = getApplicationContext().getSharedPreferences("jam",
            MODE_PRIVATE);

    conDec = new ConnectionDetector(this);

    SupportMapFragment smf = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.mapView);
    gMap = smf.getMap();

    gMap.setMyLocationEnabled(true);
    gMap.getUiSettings().setCompassEnabled(true);
    // gMap.setTrafficEnabled(true);

    // ** Gaurav Works Start Here

    gMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng arg0) {

            // Getting the Latitude and Longitude of the touched location
            latLng = arg0;

            // Clears the previously touched position
            gMap.clear();

            // Animating to the touched position
            gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));


            // Adding Marker on the touched location with address
            new ReverseGeocodingTask(getBaseContext()).execute(latLng);
        }
    });

    // ** Hide By Gaurav
     gMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
     public void onInfoWindowClick(Marker marker) {
     System.out.println(marker.getPosition().latitude);

     Intent i = new Intent(Map.this, Detail.class);

     startActivity(i);
     }
     });
    getInfo();

    CameraPosition cp = new CameraPosition.Builder()
            .target(new LatLng(Double.parseDouble("30.7353"), Double
                    .parseDouble("76.7911"))).zoom(16).build();
    gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
}

// ** Gaurav Work Start Here
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
    Context mContext;

    public ReverseGeocodingTask(Context context) {
        super();
        mContext = context;
    }

    // Finding address using reverse geocoding
    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(mContext);
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;
        List<Address> addresses = null;
        String addressText = "";
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);

            addressText = String.format("%s, %s, %s",address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getLocality(),address.getCountryName());
        }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return addressText;
    }


    // @Override
    protected void onPostExecute(String addressText) {
        selectedLocAddress = addressText;

        int caller = getIntent().getIntExtra("button", 0);
        System.out.println(caller);
        switch (caller) {
        case R.id.btMap:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_darkblue)).title(selectedLocAddress));
            System.out.println("selectedLocAddress");
            break;
        case R.id.imageButton1:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_bue)).title(selectedLocAddress));
            break;
        case R.id.imageButton2:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_green)).title(selectedLocAddress));
            break;
        case R.id.imageButton3:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_light)).title(selectedLocAddress));
            break;
        case R.id.imageButton4:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_purple)).title(selectedLocAddress));
            break;
        case R.id.imageButton5:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_red)).title(selectedLocAddress));
            break;
        case R.id.imageButton6:
            gMap.addMarker(new MarkerOptions().position(latLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.pin_annotation_yellow)).title(selectedLocAddress));
            break;
        }
    //
    // // This will be displayed on taping the marker
    // markerOptions.title(addressText);
    //
    // // Placing a marker on the touched position
    // gMap.addMarker(markerOptions);
    //
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top