Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • android google map is showing only one marker using volley library

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.52k
    Answer it

    I am using Google Map inside the "Fragment" using sqlite database connection through volley library.

    I have 4 location, but my map is showing only one marker. I don't know where exactly getting wrong.

    Where do I need to change in my code to get all 4 location markers?

    My PHP file:

            <?php
    
             define('&#95;&#95;ROOT&#95;&#95;', dirname(dirname(&#95;&#95;FILE&#95;&#95;))); 
           require&#95;once(&#95;&#95;ROOT&#95;&#95;.'/public&#95;html/Config.php');
    
    
              // Connecting to mysql database
              $mysqli = new mysqli(DB&#95;HOST, DB&#95;USER, DB&#95;PASSWORD, DB&#95;DATABASE);
    
                // json response array
                  $response = array("error" => FALSE);
    
    
    
    
                // get the tree details for google map marker
                  if($stmt = $mysqli->query("SELECT * FROM tree")){
    
    
    
                   if ($stmt->num&#95;rows) {
    
    
       while($tree = $stmt->fetch&#95;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&#95;encode($response),'<br>';
    
    
               } 
      }else {
         // user is not found with the credentials
    $response["error"] = TRUE;
    $response["error&#95;msg"] = "Tree list view credentials are wrong. Please    try again!";
    echo json&#95;encode($response);
    }
     $mysqli->close();
    

    }

    The output of PHP File: http://plantnow.net16.net/googlemaplocations.php

      {"error":false,"tree":                                                     {"treeid":"2","treespecies":"Hatti","treelatitude":"5.5","treelongitude":"5.5"}}
          {"error":false,"tree":  {"treeid":"1","treespecies":"Kattoda","treelatitude":"15.4030008","treelongitude":"75.0770729"}}
          {"error":false,"tree":{"treeid":"66","treespecies":"Sampige","treelatitude":"15.4584362","treelongitude":"74.992202"}}
              {"error":false,"tree":  {"treeid":"99","treespecies":"Halasu","treelatitude":"0","treelongitude":"0"}}
    

    My Fragmentactivity.java

      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&#95;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&#95;LONG).show();
    
    
    
            } else {
    
                Toast.makeText(getActivity().getApplicationContext(), "Location are not available ", Toast.LENGTH&#95;LONG).show();
            }
    
        }
    });
    
    return v;
    

    }

      private void getLocations() {
    // Tag used to cancel the request
    String tag&#95;string&#95;req = "req&#95;location";
    
     // pDialog.setMessage("Logging in ...");
     // showDialog();
    
    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL&#95;MAPLOCATION, new Response.Listener<String>() {
    
        @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");
    
                // Check for error node in json
                if (!error) {
    
                    for (int tree = 0; tree < jObj.length(); tree++) {
    
    
                        JSONObject location = jObj.getJSONObject("tree");
                        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);
    
                        mMap.addMarker(new MarkerOptions().title(treeid)
                                        .position(new LatLng(treelatitude, treelongitude))
                        );
    
                        // 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(4));
    
                    }
    
                    // Launch main activity
    
                } else {
                    // Error in login. Get the error message
                    String errorMsg = jObj.getString("error&#95;msg");
                    Toast.makeText(getActivity().getApplicationContext(),
                            errorMsg, Toast.LENGTH&#95;LONG).show();
                }
            } catch (JSONException e) {
                // JSON error
                e.printStackTrace();
                Toast.makeText(getActivity().getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH&#95;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&#95;LONG).show();
            hideDialog();
        }
    }) {
    
    
    
    };
    
      // Adding request to request queue
      AppController.getInstance().addToRequestQueue(strReq, tag&#95;string&#95;req);
          }
    

 1 Answer(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: