title: Retrieve location from server and show in Google map marker using volley library?
body: <p>I want to retrieve all the latitude and longitude location  from server which is already stored and show in google map marker</p>
  
  <p>My server file: <a href="http://plantnow.net16.net/googlemaplocations.php" rel="nofollow">http://plantnow.net16.net/googlemaplocations.php</a></p>
  
  <p>But I am able to get only one marker in google map i.e treeid = 2, rest of location not showing any markers in Map.</p>
  
  <p>Where is I am getting wrong in my code below, HELP ME!</p>
  
  <pre><code>   btMap.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              String search = String.valueOf(inputSearch.getSelectedItem()).toString();
  
              mMap = mMapView.getMap();
  
              mMap.clear();
  
              //getSelectedLocations(search);
  
              Toast.makeText(getActivity().getApplicationContext(), "Locations of " + search + " trees", Toast.LENGTH_LONG).show();
  
  
              if (search.equals("All Locations")) {
                  double latitude = 0;
                  double longitude = 0;
                  String mark;
  
                  getLocations();
  
                  Toast.makeText(getActivity().getApplicationContext(), "All trees locations ", Toast.LENGTH_LONG).show();
  
  
  
              } else {
  
                  Toast.makeText(getActivity().getApplicationContext(), "Location are not available ", Toast.LENGTH_LONG).show();
              }
  
          }
      });
  
      return v;
  }
  </code></pre>
  
  <p>I am using volley library to retreive location from server.</p>
  
  <pre><code> private void getLocations() {
      // Tag used to cancel the request
      String tag_string_req = "req_location";
  
     // pDialog.setMessage("Logging in ...");
     // showDialog();
  
      StringRequest strReq = new StringRequest(Request.Method.POST,
              AppConfig.URL_MAPLOCATION, new Response.Listener&lt;String&gt;() {
  
          @Override
          public void onResponse(String response) {
              Log.d(TAG, "Location Response: " + response.toString());
              hideDialog();
  
              try {
                  JSONObject jObj = new JSONObject(response);
                  boolean error = jObj.getBoolean("error");
                  List&lt;Marker&gt; markers = new ArrayList&lt;Marker&gt;();
  
                  // Check for error node in json
                  if (!error) {
  
                      JSONObject location = jObj.getJSONObject("tree");
  
                      for (int i = 0; i &lt; location.length(); i++) {
  
                          String treeid = location.getString("treeid");
                          String treespecies = location.getString("treespecies");
                          Double treelatitude = location.getDouble("treelatitude");
                          Double treelongitude = location.getDouble("treelongitude");
  
                          LatLng latlng = new LatLng(treelatitude, treelongitude);
  
                          Marker marker =  mMap.addMarker(new MarkerOptions().title(treeid)
                                          .position(new LatLng(treelatitude, treelongitude))
                          );
                             markers.add(marker);
                          // Moving CameraPosition to last clicked position
                          mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
  
                          // Setting the zoom level in the map on last position is clicked
                          mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
  
                          mMap.animateCamera(CameraUpdateFactory.zoomTo(2));
  
                      }
  
                      // Launch main activity
  
                  } else {
                      // Error in login. Get the error message
                      String errorMsg = jObj.getString("error_msg");
                      Toast.makeText(getActivity().getApplicationContext(),
                              errorMsg, Toast.LENGTH_LONG).show();
                  }
              } catch (JSONException e) {
                  // JSON error
                  e.printStackTrace();
                  Toast.makeText(getActivity().getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
              }
  
          }
      }, new Response.ErrorListener() {
  
          @Override
          public void onErrorResponse(VolleyError error) {
              Log.e(TAG, "Login Error: " + error.getMessage());
              Toast.makeText(getActivity().getApplicationContext(),
                      error.getMessage(), Toast.LENGTH_LONG).show();
              hideDialog();
          }
      }) {
  
  
  
      };
  
      // Adding request to request queue
      AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
  }
  </code></pre>
  
  <p>My PHP code: </p>
  
  <pre><code>&lt;?php
  define('__ROOT__', dirname(dirname(__FILE__))); 
  require_once(__ROOT__.'/public_html/Config.php');
  
  
  
  
   // Connecting to mysql database
      $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE);
  
     // json response array
         $response = array("error" =&gt; FALSE);
  
  
  
  
  // get the tree details for google map marker
  if($stmt = $mysqli-&gt;query("SELECT * FROM tree where `treecondition` IN ( 'Balanced', 'Healthy', 'Imbalance', 'Dangerous', 'Transplanted', 'Dieseased')")){
  
  
  
   if ($stmt-&gt;num_rows) {
  
  
         while($tree = $stmt-&gt;fetch_assoc()) {
  
          $response["error"] = FALSE;
  
          $response ["tree"] ["treeid"] = $tree['treeid'];
          $response ["tree"] ["treespecies"] = $tree['treespecies'];
          $response ["tree"] ["treelatitude"] = $tree['treelatitude'];
          $response ["tree"] ["treelongitude"] = $tree['treelongitude'];
          echo json_encode($response),'&lt;br&gt;';
            } 
             }else {
       // user is not found with the credentials
           $response["error"] = TRUE;
         $response["error_msg"] = "Tree list view credentials are wrong. Please try again!";
          echo json_encode($response);
         }
           $mysqli-&gt;close();
              }
  </code></pre>