android - 谷歌地图 V2 给出先前触摸位置的地址

标签 android google-maps location reverse-geocoding

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

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

最佳答案

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

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

关于android - 谷歌地图 V2 给出先前触摸位置的地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19675163/

相关文章:

android - adb shell 说没有找到设备,但是 adb -s <serialno> shell 工作

android - 从 Android TV 中的 BrowseFragment 中移除上边距

javascript - 颜色组合的绘图路径

mysql - 从点表中使用 MySQL 查找最近的点

mysql - sql查询在另一个查询中按经纬度查找用户位置

Android:在新线程中使用retrofit时出现NetworkOnMainThreadException?

android - 为什么 Android 项目在 Android Studio 中无法正常打开?

android - 找不到类 'com.google.android.gms.location.internal.ParcelableGeofence' ,引用自方法 glt.a

android - 在 Google 室内地图上显示地名

sql - 查询SQL数据库,如何获取用户位置附近的最近点?