2017-04-01 2 views
0

JSON에서 Map Marker를 MapActivity로 가져 오려고합니다. JSON에서 첫 번째 정보를 얻습니다. 다른 정보는 내지도에 표시되지 않습니다. 내 문제를 어떻게 해결할 수 있습니까? 여기 내지도 활동입니다 :그냥 하나의 DataObject를 JSON에서 수신

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 
private ProgressDialog pDialog; 
private GoogleMap mMap; 
private static String url="http://partypeople.bplaced.net/markers.json"; 
ArrayList<HashMap<String, String>> contactList; 
private String TAG = MainActivity.class.getSimpleName(); 
double latitude; 
double longitude; 
String namec; 

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 


    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_test); 
    contactList = new ArrayList<>(); 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map3); 
    mapFragment.getMapAsync(this); 


} 

private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 


    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     HttpHandler sh = new HttpHandler(); 


     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 


       JSONArray contacts = jsonObj.getJSONArray("markers"); 


       for (int i = 0; i < contacts.length(); i++) { 
        JSONObject c = contacts.getJSONObject(i); 

        String id = c.getString("id"); 
        String name = c.getString("name"); 
        String address = c.getString("address"); 
        String lat = c.getString("lat"); 
        String lng = c.getString("lng"); 




        // tmp hash map for single contact 
        HashMap<String, String> contact = new HashMap<>(); 

        // adding each child node to HashMap key => value 
        contact.put("id", id); 
        contact.put("name", name); 
        contact.put("address",address); 
        contact.put("lat", lat); 
        contact.put("lng", lng); 

        latitude = Double.parseDouble(lat); longitude = Double.parseDouble(lng); 
        contactList.add(contact); 
        namec = name; 


       } 
      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 
     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get json from server. Check LogCat for possible errors!", 
          Toast.LENGTH_LONG) 
          .show(); 
       } 
      }); 

     } 

     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 

      LatLng P1 = new LatLng(latitude, longitude); 

      mMap.addMarker(new MarkerOptions().position(P1).title(namec)); 
      mMap.moveCamera(CameraUpdateFactory.newLatLng(P1)); 

    } 

} 
@Override 
public void onMapReady(GoogleMap googleMap) { 

    mMap = googleMap; 
    new GetContacts().execute(); 
    mMap.setMyLocationEnabled(true); 





} 

}

여기 내 JSON입니다 :

{ 
"markers": [ 

    { 
      "id": "c201", 
      "name": "Munic", 
      "address": "try", 
      "lat": "48.142281", 
      "lng" : "11.550613" 


    }, 
    { 
      "id": "c205", 
      "name": "Berlin", 
      "address": "test", 
      "lat": "43.142281", 
      "lng" : "11.550613" 


    } 

] }

여기 내 HttpHandler를 수 있습니다 : 공공

HttpHandler 클래스 {

01 23,516,
private static final String TAG = HttpHandler.class.getSimpleName(); 

public HttpHandler() { 
} 

public String makeServiceCall(String reqUrl) { 
    String response = null; 
    try { 
     URL url = new URL(reqUrl); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     // read the response 
     InputStream in = new BufferedInputStream(conn.getInputStream()); 
     response = convertStreamToString(in); 
    } catch (MalformedURLException e) { 
     Log.e(TAG, "MalformedURLException: " + e.getMessage()); 
    } catch (ProtocolException e) { 
     Log.e(TAG, "ProtocolException: " + e.getMessage()); 
    } catch (IOException e) { 
     Log.e(TAG, "IOException: " + e.getMessage()); 
    } catch (Exception e) { 
     Log.e(TAG, "Exception: " + e.getMessage()); 
    } 
    return response; 
} 

private String convertStreamToString(InputStream is) { 
    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line).append('\n'); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

} 귀하의 문제가 onPostExecute에

답변

0

, 당신은 매핑 할 단 하나의 마커를 추가 할 수 있습니다. 당신은 거기 contactList를 사용해야합니다

for (HashMap<String, String> contact: contactList) { 
    LatLng P1 = new LatLng(
        Double.parseDouble(contact.get("lat")), 
        Double.parseDouble(contact.get("lng"))); 

    mMap.addMarker(new MarkerOptions().position(P1).title(contact.get("name"))); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(P1)); 
} 
+0

내가 그것을 시도했지만 실행 나던, 표시 한 마커가 –

+0

죄송합니다, 나는 SECON 실수를했다 지금은 잘 작동 :) –