2017-04-01 24 views
0

我尝试将地图标记从JSON获取到MapActivity中。 我刚刚从JSON中获得第一个,其他人未显示在我的地图中。 我该如何解决我的问题。 这里是我的地图活动:只需从JSON接收一个Dataobject

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:

公众class HttpHandler {

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(); 
} 

}

回答

0

你的问题是onPostExecute,只添加一个标记映射。 你应该有使用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

对不起,现在它的做工精细,我有一个塞康错误:) –