この小さなアプリを作成して、maps api v2 meth addCircle を使用して円を追加することにより、フィールドで噴霧器でカバーしたエリアを追跡しました。OnClickListener と map.clear メソッドを使用してマップをクリアする方法を理解しました。
onclicklistener でサークルの追加を停止 (一時停止) し、onclicklistener でサークルの追加を再開できるようにしたいと考えています。これが私のコードに統合されたように見えるかもしれないことを誰でも知っています:
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);
}
更新これらのブールifステートメントを追加しました:
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;};
onClick(View v) にしかし、私はそれらが私のものに渡されているとは思わない
if (setIt == true){
mMap.addCircle(circleOptions);}
一時停止ボタンで円の追加を停止しないため、OnLocationChanged 内に見つかりました。
どうすればこれを渡すことができますか? ここに私が持っているものがあります:
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);}
}