0

この小さなアプリを作成して、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);}   

}
4

1 に答える 1

1

ブール値を設定するだけです。停止した場合は、円を追加するコードを実行しないでください。停止していない場合は、コードを実行します...

編集

あなたのコードスタイルはひどいので、ここでいくつかのヒントを指摘させてください

これを決してしないでください

if (setIt == true)

if true == true と言うようなものですif(setIt)

このすべてのもので

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

if/else ステートメントを使用する必要がありますが、ビュー ID に基づいて switch ステートメントを使用することをお勧めします。inline is ステートメントも読みにくく、デバッグしたいときにコードを適切にステップ実行できます。

質問に戻ります... onLocationChanged で、これを行う必要があります..

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

コードを見て、何が起こっているかを確認してください。

于 2013-09-23T19:34:36.923 に答える