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