简体   繁体   English

如何通过哈希图将多个标记添加到Google Map API? -(Android)

[英]How to add multiple markers to the Google map API via hashmap? - (Android)

I stored latitudes and longitudes of all locations in MySQL database. 我将所有位置的纬度和经度存储在MySQL数据库中。 I get the data from database and parse it with jSON. 我从数据库中获取数据,并使用jSON进行解析。 I store all parsed values in HashMap collection. 我将所有解析后的值存储在HashMap集合中。 However when I iterate through HashMap collection I can get only last key/value (NOT all of them). 但是,当我遍历HashMap集合时,我只能得到最后一个键/值(不是全部)。 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: 这是我的json输出:

{
   "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. 我认为这是您使用hashMap的方式。

if markers.length() give more than 1 then means its loading the markers array correctly. 如果markers.length()的值大于1,则表示其正确加载了markers数组。

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 看一下这些示例,希望对您有所帮助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. 另外,要检查这一点,您可以找出hashMap中有多少个元素,我假设您只有1个元素。

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 祝一切顺利

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM