2016-08-29 9 views
-1

私は、配列の要素の回復の助けを必要としています。次のように配列される。API GoogleマップのAndroid

"html_attributions" : [], 
    "results" : [ 
     { 
     "geometry" : { 
      "location" : { 
       "lat" : 39.888397, 
       "lng" : 18.3676679 
      }, 
      "viewport" : { 
       "northeast" : { 
        "lat" : 39.8884393, 
        "lng" : 18.36771060000001 
       }, 
       "southwest" : { 
        "lat" : 39.88832449999999, 
        "lng" : 18.367643 
       } 
      } 
     }, 
     "icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/bar-71.png", 
     "id" : "400a0aa82aa68702805b140668bc2a0f70af6fa5", 
     "name" : "Cazzato Antonio", 
     "place_id" : "ChIJP0xQfHAMRBMRqB7G_EkLoLg", 
     "reference" : "CnRjAAAAZBSAI5o3K8-50oixKMxXMCGprRBNUczNCewKyqW_y3pR_K9kQSI-n_8CqFuYHn9Qe2keVO40G-pA-1y3H0S03sMCwnl5ej_lSGUJ8rLJXZtNB0wFdwN-tisEscBMEllewKj1DBcUks5gDal8sFPdFhIQ2YjqtQKkcwaIl-tEI-yn9BoU25RzXj76NptKIhxyzAvwExBUHtw", 
     "scope" : "GOOGLE", 
     "types" : [ "bar", "point_of_interest", "establishment" ], 
     "vicinity" : "Piazza Umberto I, 34, Corsano" 
     } 
    ], 
    "status" : "OK" 
} 

I場所に緯​​度と経度、その緯度とLNGを取得する必要があります。 このコードを使用して、名前と住所を取得するだけです。経度と緯度を回復するにはどうしたらいいですか?コードはありますか、あらかじめありがとうございます。

protected void onPostExecute(String result) { 

      try { 

       JSONObject jsonRootObject = new JSONObject(result); 
       JSONArray places = jsonRootObject.getJSONArray("results"); 

       for (int i=0; i< places.length(); i++) { 

        JSONObject place = places.getJSONObject(i); 
        String name = place.getString("name"); 
        String vicinity = place.getString("vicinity"); 

        TextView name_text = (TextView) rootView.findViewById(R.id.name); 
        name_text.setText(name); 

        TextView vicinity_text = (TextView) rootView.findViewById(R.id.vicinity); 
        vicinity_text.setText("In " + vicinity); 
       } 

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

答えて

0
protected void onPostExecute(String result) { 
     try { 

      JSONObject jsonRootObject = new JSONObject(result); 
      JSONArray places = jsonRootObject.getJSONArray("results"); 

      for (int i=0; i< places.length(); i++) { 

       JSONObject place = places.getJSONObject(i); 
       String name = place.getString("name"); 
       String vicinity = place.getString("vicinity"); 


       // get geomtry JSON 
       JSONObject geometry = place.getJSONObject("geometry"); 
       // get location JSON 
       JSONObject location = geometry.getJSONObject("location"); 
       // get location Doubles 
       double lat = location.getDouble("lat"); 
       double lng = location.getDouble("lng"); 



       TextView nome = (TextView) rootView.findViewById(R.id.name); 
       nome.setText(name); 

       TextView indirizzo = (TextView) rootView.findViewById(R.id.indirizzo); 
       indirizzo.setText("In " + vicinity); 
      } 

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

私はあなたの方法の途中でコードの4行を追加しました。

ロジックは、私はJSONObjectを取って、そこから得たことで、私は「場所」と呼ばれる別のJSONを取り、このJSONから「幾何学」と呼ばれる別のJSONObject、。

あなたが場所の内部で見ることができるように私はGetDoubleをメソッドを使用しますので、我々は緯度とLNGを持っています。

私は私が喜んで役立つエラー/問題があれば私の説明は、thsiフォーラムでの私の最初の答えは十分あると思います。

+0

は、私は答えに感謝するが、私は少しミスを取得:JSONObjectジオメトリ= places.getJSONObject(「幾何学」); エラー: 'Integer.parseInt' なぜこれを使用してラップ? – Francesco

+0

トレーストラックを投稿できますか? –

+0

Ohh私は問題が誤って "場所"の代わりに "場所"を書いたことがわかった、私はコードを変更し、今すぐ使用しようとする。 –

関連する問題