0

JSONパーサーから緯度と経度のマーカーでGoogleMapを作成しようとしました... AsyncTaskで試しましたが、うまくいきません..これが私のコードです。あなたの助けに感謝します: AsyncTask を使用した Json URL

 @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        // Hashmap for ListView
        magasinsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread
        new LoadAllProducts().execute();

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received 
            // means user edited/deleted product
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }

    /**
     * Background Async Task to Load all product by making HTTP Request
     * */
    class LoadAllProducts extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(mapActivity.this);

            pDialog.setMessage("Loading");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

        /**
         * getting All products from url
         * */
        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

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

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

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

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

                        // Storing each json item in variable
                        String id = c.getString(TAG_PID);
                        String nom = c.getString(TAG_NAME);
                        String ville = c.getString(TAG_VILLE);
                        String latitude = c.getString(TAG_LATITUDE);
                        String longitude = c.getString(TAG_LONGITUDE);
                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_PID, id);
                        map.put(TAG_NAME, nom);
                        map.put(TAG_VILLE, ville);
                        map.put(TAG_LATITUDE, latitude);
                        map.put(TAG_LONGITUDE, longitude);
                        // adding HashList to ArrayList
                        magasinsList.add(map);


                    }
                } else {
                    // no products found
                    // Launch Add New product Activity
                //  Intent i = new Intent(getApplicationContext(),
                        //  NewProductActivity.class);
                    // Closing all previous activities
                //  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //  startActivity(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }
        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            /**
             * Updating parsed JSON data into ListView
             * */
            double latg=Double.parseDouble(TAG_LATITUDE);
            double longg=Double.parseDouble(TAG_LONGITUDE);
            final LatLng local = new LatLng(latg,longg);
            Marker locations = googleMap.addMarker(new MarkerOptions()
            .position(local)
            .title(TAG_NAME)
            .snippet(TAG_VILLE)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.gps)));
//CameraUpdate center=
// CameraUpdateFactory.newLatLng(new LatLng(latitude,longitude));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(5);

//googleMap.moveCamera(center);
googleMap.animateCamera(zoom);
            }

編集:これはlogcatが表示したものです

 06-08 00:07:50.979: E/AndroidRuntime(5396): FATAL EXCEPTION: main
06-08 00:07:50.979: E/AndroidRuntime(5396): java.lang.NullPointerException
06-08 00:07:50.979: E/AndroidRuntime(5396):     at com.example.mercure_fidelity.mapActivity$LoadAllProducts$1.run(mapActivity.java:180)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.app.Activity.runOnUiThread(Activity.java:4170)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at com.example.mercure_fidelity.mapActivity$LoadAllProducts.onPostExecute(mapActivity.java:177)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at com.example.mercure_fidelity.mapActivity$LoadAllProducts.onPostExecute(mapActivity.java:1)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.os.AsyncTask.finish(AsyncTask.java:602)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.os.AsyncTask.access$600(AsyncTask.java:156)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.os.Looper.loop(Looper.java:137)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at android.app.ActivityThread.main(ActivityThread.java:4441)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at java.lang.reflect.Method.invokeNative(Native Method)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at java.lang.reflect.Method.invoke(Method.java:511)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-08 00:07:50.979: E/AndroidRuntime(5396):     at dalvik.system.NativeStart.main(Native Method)
06-08 00:07:51.669: D/dalvikvm(5396): GC_CONCURRENT freed 1K, 7% free 13036K/13895K, paused 2ms+3ms

編集 II :アプリケーションは停止しませんが、他の場所のマーカーではなく、自分の場所のみが表示されます...そのように postExecute メソッドのコードを変更しました...しかし、アクセスしようとするとアプリケーションが再びクラッシュしますマップへ。これはコードです(結果はマガシンのリストですresults.addAll((Collection<? extends magasin>) map);:)

protected void onPostExecute(String file_url) {

                runOnUiThread(new Runnable() {
                    public void run() {
                        if (results!=null && results.size() > 0)
                            {

                                for(int i=0; i<magasinsList.size(); i++)
                                {


                                magasin g=results.get(i);
                                double latg=Double.parseDouble(g.latitude);
                                double longg=Double.parseDouble(g.longitude);
                                final LatLng local = new LatLng(latg,longg);
                                Marker locations = googleMap.addMarker(new MarkerOptions()
                                .position(local)
                                .title(g.nom)
                                .snippet(g.ville)
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.gps)));
    //  CameraUpdate center=
           //   CameraUpdateFactory.newLatLng(new LatLng(latg,longg));
          CameraUpdate zoom=CameraUpdateFactory.zoomTo(5);

        //  googleMap.moveCamera(center);
          googleMap.animateCamera(zoom);
4

1 に答える 1