【问题标题】:Pass Lat/Lng to onPostExecute in android将 Lat/Lng 传递给 android 中的 onPostExecute
【发布时间】:2014-07-18 08:30:34
【问题描述】:

我正在尝试从 json 在 android 中创建谷歌地图,但无法从 AsyncTask 获取 onPostExecute 中的 latlng 值。我是新手。 这是我的代码。

 // Declare Variables
 JSONObject jsonobject;
 JSONArray jsonarray;
 ListView listview;
 ListViewAdapter adapter;
 ProgressDialog mProgressDialog;
 ArrayList<HashMap<String, String>> arraylist;
 static String ID="id";
 static String NAME = "name";
 static String LAT="lat";
static String LNG="lng";



@Override
    protected String doInBackground(String... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("https://sitename.com/storeListJson.php");

        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("contacts");

            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("id", jsonobject.getString("id"));
                map.put("name", jsonobject.getString("name"));

                map.put("lat",jsonobject.getString("lat"));
                map.put("lng",jsonobject.getString("lng"));

                String latt=jsonobject.getString(LAT);
                String lngg=jsonobject.getString(LNG);

                // Set the JSON Objects into the arrayddf
            //  arraylist.add(map);



                Log.d("Location", "Location:" + latt + " " +  lngg);
                //above line shows me the lat/lng values quite right in logcat 

            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }



    @Override    

    protected void onPostExecute(String result)
    {


    //what to write here, tried all things but cant get lat/lng vals


    }

我尝试了很多方法,通常得到空白地图,然后是 NPE 错误,导致 onPostExecute 缺少纬度/经度值。

Log.d("Location", "Location:" + latt + " " +  lngg);

上面一行显示了我在 doInBackground 中执行的 logcat 中的 lat/lng 值。

我是 android 的新手,希望 SO 社区能帮助我引导我走上正确的道路。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    您只需要在doInBackground() 方法中传递latt 和lngg 的值而不是null。

    @Override
    protected String doInBackground(String... params) {
        // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions
                .getJSONfromURL("https://sitename.com/storeListJson.php");
    
        try {
            // Locate the array name in JSON
            jsonarray = jsonobject.getJSONArray("contacts");
    
            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject jsonobject = jsonarray.getJSONObject(i);
                // Retrive JSON Objects
                map.put("id", jsonobject.getString("id"));
                map.put("name", jsonobject.getString("name"));
    
                map.put("lat",jsonobject.getString("lat"));
                map.put("lng",jsonobject.getString("lng"));
    
                String latt=jsonobject.getString(LAT);
                String lngg=jsonobject.getString(LNG);
    
                // Set the JSON Objects into the arrayddf
                arraylist.add(map);
    
    
                Log.d("Location", "Location:" + latt + " " +  lngg);
                //above line shows me the lat/lng values quite right in logcat 
    
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return arraylist;
    }
    
    
    
    @Override    
    
    protected void onPostExecute(ArrayList<HashMap<String, String>> results)
    {
         //Do what you want with the results here
    
    }
    

    您还必须转到扩展AsyncTask 的部分并对其进行更改,使其还包括arraylist,类似于extends AsyncTask&lt;String, null, ArrayList&lt;HashMap&lt;String, String&gt;&gt;&gt;

    【讨论】:

    • 感谢您的指导。我已经能够在 onPostExecute 中获得 lat/lng(LogCat 显示所有条目)。但是,当我尝试绘制地图时,什么也没有发生。 setUpMap(); for (int i = 0; i &lt; arraylist.size(); i++) { double lattDouble = Double.parseDouble(arraylist.get(i).get(LAT)); double longDouble =Double.parseDouble(arraylist.get(i).get(LNG)); Log.d("Location", "Location:" + lattDouble + " " + longDouble); gMap.addMarker(new MarkerOptions().position(new LatLng(lattDouble,longDouble))); 我看不到任何标记,logcat 显示 1 latlng
    • 我终于可以用标记显示地图了。我一直在两个地方调用 mapIntialize 函数,第二个地方使用代码语句。也许忘记了我已经调用了两次。非常感谢您的支持。操作系统社区震撼!
    • 请指导我或指导我如何仅显示符合用户半径限制的 20 个地点。我知道它需要位置监听器,但不知道从哪里开始。
    • 可能有几种方法可以做到这一点。这里有一些关于高级地图功能的指南:developers.google.com/maps/documentation/android/utility/… 此外,位置的文档非常好:developer.android.com/training/location/index.html
    • @Shadesblade 我在谷歌地图 v2 中需要帮助,我需要使用 json 解析将 LatLng 的值传递给具有 json 对象 LatLng 的 jsonarray。你能指导我吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-28
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多