0

I wrote this little app to track the area I've covered with my sprayer in the field by adding circles with the maps api v2 meth addCircle. I've figured out how to clear the map using an OnClickListener and the map.clear method.

I want to be able to stop adding circles (pause) with an onclicklistener and resume adding circles with an onclicklistener. Anyone have any idea what this might look like integrated into my code:

public class MainActivity extends Activity implements LocationListener, OnClickListener {


GoogleMap mMap;
Location myLocation;
EditText length;
String lengthString;
LocationManager locationmanager;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button start = (Button)findViewById(R.id.button1);
    start.setOnClickListener(this);

    Button clear = (Button)findViewById(R.id.button2);
    clear.setOnClickListener(this);

     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    if (mMap!= null) {

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    mMap.setMyLocationEnabled(true);
    mMap.animateCamera(CameraUpdateFactory.zoomBy(17));}

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub



    LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);


    locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

     length = (EditText) findViewById(R.id.editText1); 

        lengthString = length.getText().toString();

        if (v.getId() == R.id.button2) { mMap.clear();};

        //if (v.getId() == R.id.buttonPause) {Some pause method}
             //if (v.getId() == R.id.buttonResume) {Some resume method}

}

@Override
public void onLocationChanged(Location location) {



     CircleOptions circleOptions = new CircleOptions()
        .center(new LatLng(location.getLatitude(), location.getLongitude()));
          double bacons = Double.parseDouble(lengthString);

          if (bacons >=0) {

      double radi = bacons * 0.3048;
    circleOptions.radius(radi); // In meters
    circleOptions.fillColor(0xffff0000);
    circleOptions.strokeWidth(0);

          } else { 

        double radi = 20 * 0.3048;
    circleOptions.radius(radi); // In meters
    circleOptions.fillColor(0xffff0000);
    circleOptions.strokeWidth(0);

    }
          mMap.addCircle(circleOptions);   

}

Update I've added these boolean if statements :

            if (v.getId() == R.id.button1) {setIt = true;};
        if (v.getId() == R.id.button2) { mMap.clear();};
        if (v.getId() == R.id.buttonPauseIt) { setIt = false;};
        if (v.getId() == R.id.buttonResume) { setIt = true;};

to the onClick(View v) however I dont think they are being passed to my

 if (setIt == true){
          mMap.addCircle(circleOptions);} 

found within my OnLocationChanged because it will not stop adding circle with the pause button.

How do I pass this? Here is what I have:

public class MainActivity extends Activity implements LocationListener, OnClickListener {


GoogleMap mMap;
Location myLocation;
EditText length;
String lengthString;
LocationManager locationmanager;
boolean setIt = true;


@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button start = (Button)findViewById(R.id.button1);
    start.setOnClickListener(this);

    Button clear = (Button)findViewById(R.id.button2);
    clear.setOnClickListener(this);

    Button resumeIt = (Button)findViewById(R.id.buttonResume);

    Button pauseIt = (Button)findViewById(R.id.buttonPauseIt);



     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    if (mMap!= null) {

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);


    mMap.setMyLocationEnabled(true);
    mMap.animateCamera(CameraUpdateFactory.zoomBy(17));}

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub



    LocationManager locationmanager = (LocationManager) getSystemService(LOCATION_SERVICE);


    locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);



     length = (EditText) findViewById(R.id.editText1); 


        lengthString = length.getText().toString();


        if (v.getId() == R.id.button1) {setIt = true;};
        if (v.getId() == R.id.button2) { mMap.clear();};
        if (v.getId() == R.id.buttonPauseIt) { setIt = false;};
        if (v.getId() == R.id.buttonResume) { setIt = true;};


}

@Override
public void onLocationChanged(Location location) {



     CircleOptions circleOptions = new CircleOptions()
        .center(new LatLng(location.getLatitude(), location.getLongitude()));
          double bacons = Double.parseDouble(lengthString);

          if (bacons >=0) {

      double radi = bacons * 0.3048;
    circleOptions.radius(radi); // In meters
    circleOptions.fillColor(0xffff0000);
    circleOptions.strokeWidth(0);

          } else { 

        double radi = 20 * 0.3048;
    circleOptions.radius(radi); // In meters
    circleOptions.fillColor(0xffff0000);
    circleOptions.strokeWidth(0);

    }
          if (setIt == true){
          mMap.addCircle(circleOptions);}   

}
4

1 回答 1

1

just set a boolean, if stopped then dont run the code that adds circles. If its not stopped then run the code...

EDIT

let me just point out some tips here because your code style is terrible

never do this

if (setIt == true)

thats like saying if true == true just do if(setIt)

with all of this stuff

if (v.getId() == R.id.button1) {setIt = true;};
    if (v.getId() == R.id.button2) { mMap.clear();};
    if (v.getId() == R.id.buttonPauseIt) { setIt = false;};
    if (v.getId() == R.id.buttonResume) { setIt = true;};

you should be using if/else statements but better would be using switch statements based on the view id. inline is statements are terrible to read too and you can step through your code properly when you want to debug.

now back to your question...in your onLocationChanged all you have to do it this..

if (setIt){
    //all code for plotting circles goes here
}else{
    //do something when you dont want to plot or just dont use an else
}

walk through your code and see what is happening.

于 2013-09-23T19:34:36.923 回答