我的代码应该检查LatLng
是否在距离当前LatLng
3000米之内。如果是,程序应该在地图上放置一个标记。但由于某种原因,它卡在无限的for
循环中。卡在无限循环
public void onStart() {
...
Log.d("how big is compareloc", "size "+compareLocations().size());// outputs : 4
for(int mm=0;mm<compareLocations().size();mm++){
HashMap<String, LatLng> test = compareLocations().get(mm);
Log.d("LatLng positon", "marker pos "+test.get(TAG_LATLNG));
if(test.get(TAG_LATLNG)!=null){
googleMap.addMarker(new MarkerOptions().position(test.get(TAG_LATLNG)).title("test"));
}
}
...
}
这是我compareLocations()这我经纬度与latlngs的列表进行比较:
public ArrayList<HashMap<String, LatLng>> compareLocations(){
LatLng mLocation;
gps = new GPSTracker(getActivity());
if(gps.canGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
mLocation = new LatLng(latitude, longitude);
Location mylocation = new Location("Test1");
mylocation.setLatitude(mLocation.latitude);
mylocation.setLongitude(mLocation.longitude);
mdatabase.open();
Cursor cCompare=mdatabase.getAllItems();
for(int melon=0;melon<cCompare.getCount();melon++){
HashMap<String, LatLng> points = new HashMap<String, LatLng>();
double DBlat = mdatabase.getlat(melon);
double DBlong = mdatabase.getlong(melon);
LatLng myco = new LatLng(DBlat, DBlong);
Location location = new Location("Test");
location.setLatitude(myco.latitude);
location.setLongitude(myco.longitude);
if(mylocation.distanceTo(location)<=3000){
points.put(TAG_LATLNG, myco);
Log.d("Checking distance", "distance less than 300 meters");
}else{
Log.d("Checking distance", "distance is greater than 300 meters");
}
closelist.add(points);
}
mdatabase.close();
} else {
gps.showSettingsAlert();
}
return closelist;
}
我会建议选择一种更具可读性的代码风格。尽管选择这些内容很大程度上是个人偏好的问题,但我在阅读代码时非常困难。诸如在分号之后放置空格,而分号之前的括号和其他文本之间的括号之前的空格和空格之间放置空格将会很长。这也可能有助于解决像'if(gps.canGetLocation())'似乎缺少一个右括号,尽管它有一个开放的问题 - 我自己更喜欢括号 - 自己的线样式,因为它使它更容易以防止这种情况。 – Invictus
它确实有一个左括号,你是否在移动设备上查看这个?我很好,我的代码是非常可读的。但我想它的个人喜好。将复查。 – crushman
我正在阅读这通过正常的SO界面。我没有在那里看到右大括号,'mdatabase.open();'后面跟着'Cursor cCompare = mdatabase.getAllItems();'。另外,我看到'else else {'这样的行难于阅读,如果你在“else”附近加了空格,那么else语句中的Log.d()调用甚至没有缩进。我不太确定为什么你对使用垂直空间似乎非常谨慎 - 我会推荐'else'至少在与前面if语句的结束括号分开的一行中,我自己。 – Invictus