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('__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" => FALSE);
// get the tree details for google map marker
if($stmt = $mysqli->query("SELECT * FROM tree")){
if ($stmt->num_rows) {
while($tree = $stmt->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),'<br>';
}
}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->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_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;
}
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<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_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);
}
1 Answer(s)