我无法从谷歌云数据存储区获取位置,并将地图位置作为Android谷歌地图上的标记

时间:2015-11-26 14:03:36

标签: android arrays json maps google-cloud-datastore

我的位置存储在Google云数据存储区中。我需要获取这些位置并将它们绘制为Android应用程序中的地图上的标记。我在下面粘贴的代码中遇到了一些错误。 我有一个AsyncTask应该加载数据库中的所有位置,并在android谷歌地图上显示位置作为标记。

class AttemptLoad extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(DonateBloodActivity.this);
        pDialog.setMessage("Attempting to load locations...");
        //pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        // Instantiate the arraylist to contain all the JSON data.
        // we are going to use a bunch of key-value pairs, referring
        // to the json element name, and the content, for example,
        // message it the tag, and "I'm awesome" as the content.

        // Building Parameters
        List<NameValuePair> params1 = new ArrayList<NameValuePair>();
        // getting JSON string from URL
        //JSONObject json = jsonParser.
        JSONObject json = jsonParser.makeHttpRequest(LOAD_INFO_URL, "GET", params1);

        // Check your log cat for JSON reponse
        Log.d("All Products: ", json.toString());

        // when parsing JSON stuff, we should probably
        // try to catch any exceptions:
        try {

            // Checking for SUCCESS TAG
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // products found
                // Getting Array of Products
                products = json.getJSONArray(TAG_POSTS);

                // looping through All Products
                for (int i = 0; i < products.length(); i++) {
                    JSONObject c = products.getJSONObject(i);

                    // Storing each json item in variable
                    loni = c.getString(TAG_LON);
                    String username = c.getString(TAG_USERNAME);
                    lati = c.getString(TAG_LAT);
                    String time = c.getString(TAG_TIME);
                    String id = c.getString(TAG_ID);
                    // creating new HashMap
                    HashMap<String, String> map = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    map.put(TAG_LON, loni);
                    map.put(TAG_LAT, lati);
                    map.put(TAG_USERNAME, username);
                    map.put(TAG_TIME, time);
                    map.put(TAG_ID, id);

                    // adding HashList to ArrayList
                    productsList.add(map);


                    la = Float.valueOf(lati);
                    lo = Float.valueOf(loni);


                    System.out.println(la);
                    System.out.println(lo);

                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(DonateBloodActivity.this, file_url, Toast.LENGTH_LONG).show();
        }
        googlemap.addMarker(new MarkerOptions()
                .position(new LatLng(la, lo))
                .title("Hello world"));
    }
}

class AttemptStore extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(DonateBloodActivity.this);
        pDialog.setMessage("Attempting to store your location...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        String success;

        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(DonateBloodActivity.this);
        String post_username = sp.getString("username", "anon");

        try {
            // Building Parameters
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            params1.add(new BasicNameValuePair("username", post_username));
            params1.add(new BasicNameValuePair("lat", lat));
            params1.add(new BasicNameValuePair("lon", lon));
            params1.add(new BasicNameValuePair("time", current));

            Log.d("request!", "starting");

            //Posting user data to script
            JSONObject json = jsonParser.makeHttpRequest(
                    UPDATE_INFO_URL, "POST", params1);

            // full json response
            Log.d("Store location attempt", json.toString());

            // json success element
            success = json.getString(TAG_SUCCESS);
            if (success.equals("SUCCESS")) {
                Log.d("Location Stored!", json.toString());
                finish();
                return json.getString(TAG_MESSAGE);
            } else {
                Log.d("Storing Location Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        if (file_url != null) {
            Toast.makeText(DonateBloodActivity.this, file_url, Toast.LENGTH_LONG).show();
        }
    }
}

private void setUpMap() {
    if (mGoogleMap == null) {
        SupportMapFragment fragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        mGoogleMap = fragment.getMap();

        if (mGoogleMap != null) {
            //code to initialize the map
            mGoogleMap.setMyLocationEnabled(true);
        }
    }
}

0 个答案:

没有答案