标志不加入适当而循环通过添加 [英] Marker is not adding properly while add through for loop
本文介绍了标志不加入适当而循环通过添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
- 标记无法正常通过循环结果 添加
- 当我给我= 2那么它加载第二个标记,否则它只是加载单个标记结果
- 您能告诉我可能是什么原因
的JSONObject的JSONObject =新的JSONObject(结果); 的for(int i = 1; I< = jsonObject.length();我++){
的JSONObject = jsonObject.getJSONObject(Integer.toString(I)); 双纬度= jsonObject.getDouble(纬度); 双LON = jsonObject.getDouble(经度); INT SNO = jsonObject.getInt(SNO); Toast.makeText(getBaseContext(),+纬度+经度+ SNO,
Toast.LENGTH_SHORT).show(); 的MarkerOptions标记=新的MarkerOptions()
.POSITION(新经纬度(纬度,经度))。标题(新)
是.snippet(在这里); googleMap.addMarker(标记); }
解决方案
但问题是试图让内时使用的是相同的的JSONObject
变量 JSONObject的
。因此,第一个循环后,第二次将试图获得的JSONObject
从内的JSONObject
不是从父的JSONObject
。
声明一个新的变量内的JSONObject
像这样
的JSONObject的JSONObject =新的JSONObject(结果);
的for(int i = 1; I< = jsonObject.length();我++){
JSONObject的jsonInnerObject = jsonObject.getJSONObject(Integer.toString(I));
双纬度= jsonInnerObject.getDouble(纬度);
双LON = jsonInnerObject.getDouble(经度);
//此处添加其他的东西
}
另一种最好的方法是使用按键进行迭代。这并不需要的索引。你可以在地方指标中的任何值。
的JSONObject的JSONObject =新的JSONObject(JSON);
迭代器<对象>迭代= jsonObject.keys();而(iterator.hasNext()){
obj对象= iterator.next();
的JSONObject innerJsonObject = jsonObject.getJSONObject(obj.toString());
如果(innerJsonObject!= NULL){
双纬度= innerJsonObject.getDouble(纬度);
双LON = innerJsonObject.getDouble(经度);
//你做其他的东西在这里添加标记
}
}
- Marker not properly adding through for loop
- When i give i=2 then it's loading 2nd marker otherwise it's just loading single marker
- Can you please tell me what could be the reason
JSONObject jsonObject = new JSONObject(result);
for (int i = 1; i <= jsonObject.length(); i++) {
jsonObject = jsonObject.getJSONObject(Integer.toString(i));
Double lat = jsonObject.getDouble("latitude");
Double lon = jsonObject.getDouble("longitude");
int sno = jsonObject.getInt("sno");
Toast.makeText(getBaseContext(), "" + lat + lon + sno,
Toast.LENGTH_SHORT).show();
MarkerOptions marker = new MarkerOptions()
.position(new LatLng(lat, lon)).title("New")
.snippet("are here");
googleMap.addMarker(marker);
}
解决方案
Problem is your using the same JSONObject
variable when trying to get the inner JSONObject
. So after the first for loop , the second will try to get the JSONObject
from inner JSONObject
not from the parent JSONObject
.
Declare a new variable for inner JSONObject
like this
JSONObject jsonObject = new JSONObject(result);
for (int i = 1; i <= jsonObject.length(); i++) {
JSONObject jsonInnerObject = jsonObject.getJSONObject(Integer.toString(i));
Double lat = jsonInnerObject.getDouble("latitude");
Double lon = jsonInnerObject.getDouble("longitude");
// Add your other stuff here
}
Another best approach is to Iterate using keys. This doesn't required the index. You can have any value in place of index.
JSONObject jsonObject = new JSONObject(json);
Iterator<Object> iterator = jsonObject.keys();
while (iterator.hasNext()){
Object obj = iterator.next();
JSONObject innerJsonObject = jsonObject.getJSONObject(obj.toString());
if(innerJsonObject != null) {
Double lat = innerJsonObject.getDouble("latitude");
Double lon = innerJsonObject.getDouble("longitude");
// do your other stuff here to add to marker
}
}
这篇关于标志不加入适当而循环通过添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文