I stored latitudes and longitudes of all locations in MySQL database. I get the data from database and parse it with jSON. I store all parsed values in HashMap collection. However when I iterate through HashMap collection I can get only last key/value (NOT all of them). What do I need to do in order to get all key/values of each location? Here is my code:
package com.example.maptest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.util.Log;
import com.google.android.gms.maps.GoogleMap;
public class ShowAllMarkers {
JSONParser jParser = new JSONParser();
private static String url_all_markers = "http://...............";
JSONParser jsonParser = new JSONParser();
JSONArray markers;
GoogleMap map;
// HashMap<String, String> markersList=new HashMap<String, String>();;
Map<String, String> hMap = new HashMap<String, String>();
public ShowAllMarkers(GoogleMap map) {
this.map = map;
}
public void getAllMarkers() {
new AsyncTask<String, String, String>() {
@Override
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_markers,
"GET", params);
// in success;
try {
int success = json.getInt("success");
if (success == 1) {
markers = json.getJSONArray("markers");
for (int i = 0; i < markers.length(); i++) {
JSONObject markersObject = markers.getJSONObject(i);
String lat = markersObject.getString("lat");
String longt = markersObject.getString("long");
hMap.put("Latitude", lat);
hMap.put("Longitude", longt);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// Outputs hashmap elements as key and value
Set<Map.Entry<String, String>> set = hMap.entrySet();
for (Map.Entry<String, String> me : set) {
Log.d("JWP", "Key is:" + me.getKey() + ", value is: " + me.getValue());
}
}
}.execute();
}
}
This is my json output:
{
"markers":[
{
"lat":"40.4176083",
"long":"49.9389495",
"title":"Location",
"description":""
},
{
"lat":"40.4175711",
"long":"49.9389274",
"title":"Location",
"description":""
},
{
"lat":"40.4176178",
"long":"49.9389621",
"title":"Location",
"description":""
},
{
"lat":"40.4176178",
"long":"49.9389621",
"title":"Location",
"description":""
},
{
"lat":"40.4176178",
"long":"49.9389621",
"title":"Location",
"description":""
},
{
"lat":"40.4176178",
"long":"49.9389621",
"title":"Location",
"description":""
},
{
"lat":"40.4176178",
"long":"49.9389621",
"title":"Location",
"description":""
},
{
"lat":"40.4176126",
"long":"49.9389561",
"title":"Location",
"description":""
}
],
"success":1
}
Thank you in advance.
I think its the way you use your hashMap.
if markers.length() give more than 1 then means its loading the markers array correctly.
Take a look at these examples, hope it helps http://java67.blogspot.com.au/2013/02/10-examples-of-hashmap-in-java-programming-tutorial.html
According to the interface, its taking in key and value pair. And in your code, you kept replacing the new value with the same key.
Also, to check this, you can find out how many elements are in the hashMap, I would assume you had 1 only.
I would expect something like this
int key=0;
Loop{
hMap.put("Latitude_key"+key, lat);
hMap.put("Longitude_key"+key, longt);
key++;
}
All the best
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.