如何在Google地图Android中清除以前绘制的圆圈?

时间:2017-09-16 11:23:30

标签: android dictionary geolocation

我希望删除之前绘制的圆圈。当我搜索新地点时,第一个圆圈仍然存在,并且还绘制了新圆圈。基本上每当位置发生变化时,圆圈都会被绘制(相互重叠),而不会删除前一个圆圈。

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_geocoding_maps);

    toolbar=(Toolbar)findViewById(R.id.mapToolbar);if (!checkPlayServices()) {
        Toast.makeText(this, "Device not compatible", Toast.LENGTH_SHORT).show();
        finish();
    }
    Bundle extras=getIntent().getExtras();
    if(extras==null)
    {
        itemAction=-1;
        Log.w(TAG, "Extras are null");
        finish();
    }
    else
    {
        try{
            item=extras.getParcelable(GeofenceUtils.P_KEY);
        }catch (Exception e)
        {
            Log.w(GeofenceUtils.getTag(), e.getMessage());
        }
        if (item != null) {
            if(item.getLatitude()==0.0&&item.getLongitude()==0.0) {
                //New item
                itemAction=0;
            }
            else {
                //Update item
                Log.w(TAG, "Title : "+item.getTitle());
                itemAction=1;
            }
            Log.w(TAG, "Item Action: "+itemAction);
        }
        else {
            Log.w(TAG, "Item null: ");
        }
    }
    dbHelper=GeofenceDBHelper.getInstance(this);
    assert toolbar != null;
    toolbar.setTitle("Pick a location");
    toolbar.setTitleTextColor(Color.WHITE);
    setSupportActionBar(toolbar);
    address="";
    addressText = (EditText) findViewById(R.id.addressText);
    if(itemAction==1) {
        address=item.getAddress();
        if (addressText != null) {
            addressText.setText(address);
        }
    }
    addressText.setOnEditorActionListener(new TextView.OnEditorActionListener() {

        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                Log.w(TAG, "onEditorAction");
                address = addressText.getText().toString();
                LatLng latLng = getLocationFromAddress(GeocodingMapsActivity.this, address);
                if (latLng != null) {

                    mlatitude = latLng.latitude;
                    mlongitude = latLng.longitude;

                    marker.setPosition(latLng);//match this behavior to your 'Send' (or Confirm) button
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
                    mMap.animateCamera(cameraUpdate);



                    circle = drawCircle(new LatLng(mlatitude, mlongitude));


                } else {
                    Log.w(TAG, "onEditorAction: address null");

                }
            }
            return false;
        }
    });
    if(savedInstanceState!=null)
    {
        //For handling screen rotations
        mlatitude=savedInstanceState.getDouble("Latitude");
        mlongitude=savedInstanceState.getDouble("Longitude");

        Log.w(TAG, "itemAction savedInstanceState: "+itemAction);
    }
    else
    {
        mlatitude=item.getLatitude();
        mlongitude=item.getLongitude();
    }

    clearButton=(ImageButton)findViewById(R.id.clear_text);
    clearButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addressText.setText("");
        }
    });

    MyLocationButton=(FloatingActionButton)findViewById(R.id.fab);
    MyLocationButton.setBackgroundTintList(ColorStateList.valueOf(Color.WHITE));
    MyLocationButton.setRippleColor(Color.parseColor("#f5f5f5"));
    MyLocationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mGoogleApiClient.isConnected()) {
                LatLng latLng = getCurrentLocation();
                if (latLng != null) {
                    setAddress();
                    CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
                    mMap.animateCamera(cameraUpdate);
                  //  circle = drawCircle(new LatLng(mlatitude, mlongitude));


                }

            }
        }
    });
    MyLocationButton.setVisibility(View.GONE);
    mGeofenceList=new ArrayList<>();
    buildGoogleApiClient();
    createLocationRequest();
    buildLocationSettingsRequest();
    checkLocationSettings();
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    Log.w(TAG, "onCreate");
}




private void removeEverything(){
    marker.remove();
    marker = null;
    circle.remove();
    circle = null;


}

/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */

Circle circle;
@Override
public void onMapReady(GoogleMap googleMap) {
    Log.w(TAG, "onMapReady1");
    mMap = googleMap;
  //  mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    enableMyLocation();
    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            marker.setPosition(latLng);
            mlatitude=latLng.latitude;
            mlongitude=latLng.longitude;
            setAddress();
        }
    });
    Log.w(TAG, "onMapReady2");
    LatLng mLocation=null;
    if(mGoogleApiClient.isConnected()&&itemAction==0)
        mLocation = getCurrentLocation();
    Log.w(TAG, "onMapReady3");
    if (itemAction==0&&mLocation != null) {
        mlatitude = mLocation.latitude;
        mlongitude = mLocation.longitude;
    }
    else if(itemAction==1) {
        mLocation=new LatLng(mlatitude, mlongitude);
    }
    else
        mLocation = new LatLng(0.0, 0.0);

    Log.w(TAG, "onMapReady3");
    if(mGoogleApiClient.isConnected())
        setAddress();
    Log.w(TAG, "onMapReady4");





    marker = mMap.addMarker(new MarkerOptions().position(mLocation).draggable(true));
    Log.w(GeofenceUtils.getTag(), item.getMarkerColor()+"");
   marker.setIcon(BitmapDescriptorFactory.fromResource(GeofenceUtils.getMarker(item.getMarkerColor())));
    mMap.setOnMarkerDragListener(this);
    if(itemAction==1)
    {
        CameraUpdate cameraUpdate=CameraUpdateFactory.newLatLngZoom(new LatLng(item.getLatitude(), item.getLongitude()), 18);
        CameraUpdateFactory.newLatLngZoom(mLocation, 18);
        mMap.animateCamera(cameraUpdate);
      //  circle = drawCircle(new LatLng(mlatitude, mlongitude));

    }
    Log.w(TAG, "onMapReady5");




}
private Circle drawCircle(LatLng latLng) {

    CircleOptions options = new CircleOptions()
            .center(latLng)
            .radius(50)

            .strokeColor(Color.BLACK)
            .fillColor(Color.rgb( 229, 204, 229))

            .strokeWidth(3);



    return mMap.addCircle(options);
}

There are two circles drawn, i want first circle to be removed as soon as the second circle is drawn.

1 个答案:

答案 0 :(得分:0)

private Circle circle;

在将圆绘制到地图之前。首先检查是否绘制了圆。只需删除圆圈,然后在地图上绘制新圆圈。

 if(circle!=null){
      circle.remove();
    }
circle = drawCircle(new LatLng(mlatitude, mlongitude));