Google地图V2提供以前触摸位置的地址

时间:2013-10-30 06:18:17

标签: android google-maps location reverse-geocoding

我在我的应用中使用谷歌地图v2。我将标记放在我点击的位置。当我再次点击地图时,私人市场消失了,并添加了新标记。当我检查地址时,它会显示已删除标记的地址。即它包含最后触摸的位置。 请帮我。如何删除地址以及删除标记。这是我的代码

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);
    //
    // }
}}

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题。这是我自己的错误。它会帮助你。

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);
    //
}
}