Google Maps API删除存储的标记

时间:2015-06-06 11:57:03

标签: android google-maps google-maps-api-3

我正在保存我使用数组放入地图的标记但是我想要一个AlertDialog出现问我是否要在我点击标记时删除有问题的标记,但我该怎么做?这是我用来存储标记的代码

            sharedPreferences = getSharedPreferences("location", 0);

        // Getting number of locations already stored
        locationCount = sharedPreferences.getInt("locationCount", 0);

        // Getting stored zoom level if exists else return 0
        String zoom = sharedPreferences.getString("zoom", "0");

        // If locations are already saved
        if(locationCount!=0){

            String lat = "";
            String lng = "";
            String title = "";
            String snippet = "";

            // Iterating through all the locations stored
            for(int i=0;i<locationCount;i++){

                // Getting the latitude of the i-th location
                lat = sharedPreferences.getString("lat"+i,"0");

                // Getting the longitude of the i-th location
                lng = sharedPreferences.getString("lng"+i,"0");

                String textstored = sharedPreferences.getString("title"+i,"0");

                String snippetstored = sharedPreferences.getString("snippet"+i,"0");

                // Drawing marker on the map
                googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(Double.parseDouble(lat), Double.parseDouble(lng)))
                        .title(textstored)
                        .snippet(snippetstored)
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

            }
    }

      googleMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {



        @Override
        public void onMapLongClick(final LatLng point) {
            AlertDialogWrapper.Builder builder = new AlertDialogWrapper.Builder(MainActivity.this);
            builder.setTitle("Titel:");

            // Set up the input
            final EditText input = new EditText(MainActivity.this);
            builder.setView(input);

            // Set up the buttons
            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    AlertDialogWrapper.Builder builderinfo = new AlertDialogWrapper.Builder(MainActivity.this);
                    builderinfo.setTitle("Info:");

                    // Set up the input
                    final EditText inputinfo = new EditText(MainActivity.this);
                    builderinfo.setView(inputinfo);

                    // Set up the buttons
                    builderinfo.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            locationCount++;

                            // Drawing marker on the map
                            String text = input.getText().toString();
                            String textinfo = inputinfo.getText().toString();
                            googleMap.addMarker(new MarkerOptions()
                                    .position(point)
                                    .title(text)
                                    .snippet(textinfo)
                                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));

                            /** Opening the editor object to write data to sharedPreferences */
                            SharedPreferences.Editor editor = sharedPreferences.edit();

                            // Storing the latitude for the i-th location
                            editor.putString("lat"+ Integer.toString((locationCount-1)), Double.toString(point.latitude));

                            // Storing the longitude for the i-th location
                            editor.putString("lng"+ Integer.toString((locationCount-1)), Double.toString(point.longitude));

                            editor.putString("title"+(locationCount-1), text);
                            editor.putString("snippet"+(locationCount-1),textinfo);

                            // Storing the count of locations or marker count
                            editor.putInt("locationCount", locationCount);


                            /** Saving the values stored in the shared preferences */
                            editor.commit();

                            Toast.makeText(getBaseContext(), "En Markör lades till framgångsrikt", Toast.LENGTH_SHORT).show();


                        }
                    });

1 个答案:

答案 0 :(得分:1)

在您实施onMapLongClick的同一个班级中实施OnMarkerClickListener

因此:

public class MyMapView implements OnMarkerClickListener { 

  ...
  @Override
  public boolean onMarkerClick(Marker marker) {
    // your code here
    return true; 
  }
...
}

然后在onMarkerClick(Marker marker)中,您可以移除标记。

使用

从共享偏好设置中删除已保存的标记
editor.remove(String key);
editor.commit();

并使用以下地图从

开始
marker.remove()

就警报而言,我建议您查看Dialogs上的开发指南