我有JSON网址,其响应类似于
{
"status": 200,
"response": [{
"latitude": "17.3850",
"longitude": "78.4867",
"name": "Hyderabad"
},
{
"latitude": "17.4399",
"longitude": "78.4983",
"name": "Secunderabad"
}]
}
我想从url获取latlang值并在地图上显示为标记。如果单击标记,则应显示标题& spinet以及我当前位置与地图下方TextView
中标记之间的距离。
请帮忙。谢谢。
答案 0 :(得分:1)
试试这个
GoogleMap map = ... // get a map.
// Add a marker at San Francisco.
Marker marker = map.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco");
private Map<K, V> multiMap = new HashMap<K, V>();
ArrayList<HashMap<String, String>> list_params = new ArrayList<HashMap<String, String>>();
JSONObject jobj = new JSONObject("Your Response");
if(jobj.has("response")
{
JSONArray object = jsonArray.getJSONArray("response");
for(int i = 0 ; i <object.isze ; i++)
{
JSONObject jsonObject = object.getJSONObject(i);
String latitude = jsonObject .getString("latitude");
String longitude= jsonObject .getString("longitude");
String name= jsonObject .getString("name");
multiMap.put("latitude ",latitude);
multiMap.put("longitude",longitude);
multiMap.put("name",name);
list_params.add(multiMap);
}
}
答案 1 :(得分:0)
要解析数据,请使用以下方法:
JSONArray response= null;
ArrayList<HashMap<String, String>> locationList= new ArrayList<HashMap<String, String>>();
// Making a request to url and getting response
String jsonStr = //The response in String format;
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
response= jsonObj.getJSONArray("response");
// looping through All records
for (int i = 0; i < response.length(); i++) {
JSONObject c = response.getJSONObject(i);
String latitude= c.getString("latitude");
String longitude= c.getString("longitude");
// tmp hashmap for single contact
HashMap<String, String> location_= new HashMap<String, String>();
// adding each child node to HashMap key => value
location_.put("latitude", latitude);
location_.put("longitude", longitude);
locationList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("Couldn't get any data from the url");
}
现在,你在 locationList hashmap中获得了所有纬度和经度。迭代它并在Google Map中绘制它们。
for(i=0;i<locationlist.size();i++)
{
HashMap<String, String> resultp = new HashMap<String, String>();
resultp = locationlist.get(i);
String lat=resultp.get("latitude");
String lon=resultp.get("longitude");
//Plot latitude and longitude in the map
}