2016-04-14 10 views
0

JSONから座標を解析し、両方の値(lat、lng)を1つの "オブジェクト"に保存する必要があります。その後、Googleマップアクティビティの最初の10個のオブジェクトをマップに表示します。1つのオブジェクトに座標が必要です

マイMainActivity

List<JSONModels> jsonModelsList = new ArrayList<>(); 

      try { 
       JSONObject jsonObject = new JSONObject(buffer.toString()); 
       JSONArray placemarks = jsonObject.getJSONArray("placemarks"); 

       for (int i = 0; i < placemarks.length(); i++) { 
        JSONObject jsonPart = placemarks.getJSONObject(i); 
        JSONModels jsonModels = new JSONModels(); 
        jsonModels.setName(jsonPart.getString("name")); 
        jsonModels.setAddress(jsonPart.getString("address")); 
        jsonModels.setExterior(jsonPart.getString("exterior")); 
        jsonModels.setInterior(jsonPart.getString("interior")); 
        jsonModels.setFuel(jsonPart.getInt("fuel")); 

        for (int j = 0; j <= 10; j++) { 
         JSONObject marker = placemarks.getJSONObject(j); 
         JSONArray coordinates = marker.optJSONArray("coordinates"); 

         double lat = coordinates.optDouble(0); 
         double lng = coordinates.optDouble(1); 

        } 

        jsonModelsList.add(jsonModels); 

       } 

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

答えて

1

ジェイドは、私たちは長い間一緒に、緯度を保存するために多くの方法がありますが、最も簡単な方法は、マーカーオブジェクトのリストを作成することですので、我々は簡単に地図上のマーカーのリストを使用することができます。

try { 
    List<LatLng> LatlongList = new ArrayList<LatLng>(); 
     JSONObject jsonObject = new JSONObject(buffer.toString()); 
     JSONArray placemarks = jsonObject.getJSONArray("placemarks"); 

     for (int i = 0; i < placemarks.length(); i++) { 
      JSONObject jsonPart = placemarks.getJSONObject(i); 
      JSONModels jsonModels = new JSONModels(); 
      jsonModels.setName(jsonPart.getString("name")); 
      jsonModels.setAddress(jsonPart.getString("address")); 
      jsonModels.setExterior(jsonPart.getString("exterior")); 
      jsonModels.setInterior(jsonPart.getString("interior")); 
      jsonModels.setFuel(jsonPart.getInt("fuel")); 

      for (int j = 0; j <= 10; j++) { 
       JSONObject marker = placemarks.getJSONObject(j); 
       JSONArray coordinates = marker.optJSONArray("coordinates"); 
       double lat = coordinates.optDouble(0); 
       double lng = coordinates.optDouble(1); 
    //add lat long object in list of LatlongList. 
     LatlongList.add(new LatLng(lat,lng)); 
      } 
    } 
`  //LatlongList is the list where the all lat and longs a together. 

    LatlongList.size(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return LatlongList; 
+0

私が探しているものは...ありがとう。地図上に結果を表示するために、私のgooglemapsのアクティビティでArraylistをLngとLatで使用できますか? – Jade

関連する問題