我有 2 个 json 文件,第一个用于类别,第二个用于生成标记并将它们放在地图中。
我的应用程序的第一个屏幕在列表视图上显示类别,当我单击其中一个类别时,它应该根据地图上选择的类别显示标记,显示地图或读取 json 没有问题
示例 json 类别:
{
"categories": [
{
"id": "01",
"categoria": "Restaurante"
}
]
}
示例 json 位置:
{
"places": [
{
"id": "01",
"lat": "20.583566571214778",
"long": "-100.37047419121245",
"dist": "100m",
"star": "1",
"icon": "03",
"name": "Restaurante San Pedro",
"cat": "Restaurante",
"sched": "9:00 - 23:00",
"text": "Una gran seleccion de platillos para su deleite."
}
]
}
问题是它不会显示标记,我使用 if 来比较选择的类别与场所 json 中的任何类别业务相同,如果相同则显示标记,我使用 toast看结果和是一样的,不知道为什么它不会执行if语句而不是else
if (cat == recuperamos_variable_cat) {
LatLng lugares = new LatLng(dlatitud, dlongitud);
Marker ubicacion2 = mapa.addMarker(new MarkerOptions().position(lugares).title(name).snippet(text));
Toast mensaje2 = Toast.makeText(getApplicationContext(), cat + recuperamos_variable_cat, Toast.LENGTH_SHORT);
mensaje2.show();
} else {
Toast mensaje2 = Toast.makeText(getApplicationContext(), "No hay coincidencias", Toast.LENGTH_SHORT);
mensaje2.show();
}
我把其余的代码:
//recover the value of the category selected
final String recuperamos_variable_id = getIntent().getStringExtra("TAG_ID");
final String recuperamos_variable_cat = getIntent().getStringExtra("TAG_CAT");
// Hashmap for ListView
ArrayList<HashMap<String, String>> placesList = new ArrayList<HashMap<String, String>> ();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
places = json.getJSONArray(TAG_PLACES);
// looping through All Contacts
for (int i = 0; i < places.length(); i++) {
JSONObject c = places.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String lat = c.getString(TAG_LAT);
String lon = c.getString(TAG_LONG);
String dist = c.getString(TAG_DIST);
String star = c.getString(TAG_STAR);
String name = c.getString(TAG_NAME);
String cat = c.getString(TAG_CAT);
String icon = c.getString(TAG_ICON);
String sched = c.getString(TAG_SCHED);
String text = c.getString(TAG_TEXT);
double dlatitud = Double.parseDouble(lat); //Convertimos valores a double
double dlongitud = Double.parseDouble(lon);
if (cat == recuperamos_variable_cat) {
LatLng lugares = new LatLng(dlatitud, dlongitud);
Marker ubicacion2 = mapa.addMarker(new MarkerOptions().position(lugares).title(name).snippet(text));
Toast mensaje2 = Toast.makeText(getApplicationContext(), cat + recuperamos_variable_cat, Toast.LENGTH_SHORT);
mensaje2.show();
} else {
Toast mensaje2 = Toast.makeText(getApplicationContext(), "No hay coincidencias", Toast.LENGTH_SHORT);
mensaje2.show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}