在android地图中获取json纬度和经度

时间:2016-03-31 09:45:56

标签: android google-maps android-mapview

我在这里使用地图从json网络服务成功获取纬度,经度和其他详细信息但在地图中只显示一个标记显示其他纬度,经度和标记未在地图中显示

class DownloadJSON extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setTitle("Loading locations...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        e = JSONfunctions
          .getJSONfromURL("http://beauty.ls.com/webservices/MapStores.php?client_id=1");
        Log.d("jsonobject", String.valueOf(e));


        try {
            // Locate the array name in JSON
            Log.d("second", "");
            jsonarray = e.getJSONArray("mapstores");
            Log.d("three","");
            for (int i = 0; i < jsonarray.length(); i++) {
                e = jsonarray.getJSONObject(i);

                 store_lat = e.getString("store_latitude");
                 store_long = e.getString("store_longitude");
                 store_id = e.getString("store_id");
                 store_name = e.getString("store_name");
                 store_address = e.getString("store_address");
                 store_city = e.getString("store_city");
                 store_phone = e.getString("store_phone");
                 store_email = e.getString("store_email");
                Log.d("latitude",store_lat);
                Log.d("longitude",store_long);

                lat1 = Double.parseDouble(store_lat);
                lng1 = Double.parseDouble(store_long);
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {

        mProgressDialog.dismiss();

            googleMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1))
                    .title(e.getString("store_id")));

    }
}

我不知道我应该在哪里做出改变请帮帮我

2 个答案:

答案 0 :(得分:1)

你需要这样做:

您的代码有什么问题:您要在PostExecute()上添加单个lat long,而不是为for循环中的所有LatLng添加代码。

class DownloadJSON extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setTitle("Loading locations...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {

        e = JSONfunctions
          .getJSONfromURL("http://beauty.ls.com/webservices/MapStores.php?client_id=1");
        Log.d("jsonobject", String.valueOf(e));
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {

        mProgressDialog.dismiss();

        try {
            // Locate the array name in JSON
            Log.d("second", "");
            jsonarray = e.getJSONArray("mapstores");
            Log.d("three","");
            for (int i = 0; i < jsonarray.length(); i++) {
                e = jsonarray.getJSONObject(i);

                 store_lat = e.getString("store_latitude");
                 store_long = e.getString("store_longitude");
                 store_id = e.getString("store_id");
                 store_name = e.getString("store_name");
                 store_address = e.getString("store_address");
                 store_city = e.getString("store_city");
                 store_phone = e.getString("store_phone");
                 store_email = e.getString("store_email");
                Log.d("latitude",store_lat);
                Log.d("longitude",store_long);

                lat1 = Double.parseDouble(store_lat);
                lng1 = Double.parseDouble(store_long);
                googleMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1))
                    .title(e.getString("store_id")));
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

    }
}

希望这会对你有所帮助。

答案 1 :(得分:0)

那你的代码中的JSON函数是什么?

无论如何你要做的是,使用jsonArray,它在后执行方法中具有来自api响应的lat long值,并遍历到数组的末尾。现在对于每次迭代,使用该迭代的lat long值在地图上调用addMarker

这种方式可确保您为相应的纬度和经度添加不同的标记。

您目前正在做的是在地图上仅添加一次标记,尽管您从响应中获得了所有lat long值。

以下是此代码段:

    @Override
    protected Void doInBackground(Void... params) {

        e = JSONfunctions
          .getJSONfromURL("http://beauty.ls.com/webservices/MapStores.php?client_id=1");
        Log.d("jsonobject", String.valueOf(e));
        return null;
    }

    @Override
    protected void onPostExecute(Void args) {

        mProgressDialog.dismiss();

        try {
            // Locate the array name in JSON
            Log.d("second", "");
            jsonarray = e.getJSONArray("mapstores");
            Log.d("three","");
            for (int i = 0; i < jsonarray.length(); i++) {
                e = jsonarray.getJSONObject(i);

                 store_lat = e.getString("store_latitude");
                 store_long = e.getString("store_longitude");
                 store_id = e.getString("store_id");
                 store_name = e.getString("store_name");
                 store_address = e.getString("store_address");
                 store_city = e.getString("store_city");
                 store_phone = e.getString("store_phone");
                 store_email = e.getString("store_email");
                Log.d("latitude",store_lat);
                Log.d("longitude",store_long);

                lat1 = Double.parseDouble(store_lat);
                lng1 = Double.parseDouble(store_long);
                googleMap.addMarker(new MarkerOptions().position(new LatLng(lat1, lng1))
                    .title(e.getString("store_id")));
            }
        } catch (JSONException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }

    }
}

在我打字的时候,@ oren已经给出了答案。 :P 希望你能得到你所需要的。