gpt4 book ai didi

java - 添加暂停和恢复 addCircle 到 map api v2

转载 作者:行者123 更新时间:2023-12-01 14:05:06 26 4
gpt4 key购买 nike

我编写了这个小应用程序,通过使用 map api v2 方法 addCircle 添加圆圈来跟踪我在田间用喷雾器覆盖的区域。我已经弄清楚如何使用 OnClickListener 和 map.clear 方法清除 map 。

我希望能够停止使用 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);

}

更新我添加了这些 boolean 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);}

}

最佳答案

只需设置一个 boolean 值,如果停止则不要运行添加圆圈的代码。如果它没有停止,则运行代码...

编辑

让我在这里指出一些提示,因为你的代码风格很糟糕

永远不要这样做

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 语句,但最好使用基于 View id 的 switch 语句。内联 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
}

浏览一下您的代码,看看发生了什么。

关于java - 添加暂停和恢复 addCircle 到 map api v2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18967290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com