php - Volley connection get all data in one time with use (Where) Conditional Sentence

I use Google Maps to display specific location. The data of location get from my database and it's already available into database. Every field in my database has own location as Latitude and Longitude.
For example
Student 1 - Seating Number - Semester - Latitude - Longitude
Student 1 - 3 - 2 - 33.8523341 - 151.2106085
like that..
The problem is Volley connection get all students location data from databases at once time .This is error, I should only get the data of chosen student (I use Conditional Sentence (Where) into url of Volley and php file to get specific data.But I don't Know Why still it's not work ).
As you can see picture below that I have two students in the database and their location points appear at same time when I order one student.
And this picture from database as you can see I have to student now :
If any one know the solution please help me I need to display different locations according to the data in the database for each student.
public class GetMapLaction extends AppCompatActivity implements OnMapReadyCallback {
GoogleMap gMap;
MarkerOptions markerOptions = new MarkerOptions();
private final LatLng mDefaultLocation = new LatLng(-33.8523341, 151.2106085);
FusedLocationProviderClient mFusedLocationProviderClient;
private static final int DEFAULT_ZOOM = 6;
private CameraPosition mCameraPosition;
private Location mLastKnownLocation;
LatLng latLng;
String title;
private boolean mLocationPermissionGranted;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private static final String KEY_CAMERA_POSITION = "camera_position";
public static final String st_ids= "st_id";
public static final String TITLE = "nama";
public static final String LAT = "Latitude";
public static final String LNG = "Longitude";
private static final String TAG = GetMapLaction.class.getSimpleName();
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
setContentView(R.layout.get_map_lcation);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
Intent i = getIntent();
final String st_id= i.getStringExtra("st_id");
textView=(TextView)findViewById(R.id.textView) ;
textView.setText(textView.getText() + st_id);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
if (gMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, gMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
super.onSaveInstanceState(outState);
}
}
@Override
public void onMapReady(GoogleMap map) {
gMap = map;
if (ContextCompat.checkSelfPermission(GetMapLaction.this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
permission!",Toast.LENGTH_SHORT).show();
mLocationPermissionGranted = true;
getMarkers();
} else {
requestStoragePermissionn();
}
}
private void requestStoragePermissionn() {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
new AlertDialog.Builder(this)
.setTitle("Permission needed")
.setMessage("This permission is needed because of this and that")
.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(GetMapLaction.this,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create().show();
} else {
ActivityCompat.requestPermissions(this,
new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getMarkers();
gMap.setMyLocationEnabled(true);
gMap.getUiSettings().setZoomControlsEnabled(true);
gMap.setMinZoomPreference(7);
Toast.makeText(this, "Permission GRANTED", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();
}
}
}
private void addMarker(LatLng latlng, final String title) {
markerOptions.position(latlng);
markerOptions.title(title);
gMap.addMarker(markerOptions);
gMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Toast.makeText(getApplicationContext(), marker.getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
private void getMarkers() {
final String url ="http://000000000/stedant/map.php?st_id=" + st_id;
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Response: ", response.toString());
try {
JSONObject jObj = new JSONObject(response);
String getObject = jObj.getString("data");
JSONArray jsonArray = new JSONArray(getObject);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
// title = jsonObject.getString(TITLE);
latLng = new LatLng(Double.parseDouble(jsonObject.getString(LAT)), Double.parseDouble(jsonObject.getString(LNG)));
addMarker(latLng, title);
gMap.animateCamera(zoomingLocation(latLng));
}
} catch (JSONException e) {
Toast.makeText(GetMapLaction.this, "This is my Toast message!", Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error: ", error.getMessage());
Toast.makeText(GetMapLaction.this, error.getMessage(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
private CameraUpdate zoomingLocation(LatLng latLng) {
return CameraUpdateFactory.newLatLngZoom(latLng, 7);
}
private void getDeviceLocation() {
try {
if (mLocationPermissionGranted) {
Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(this, new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
// Set the map's camera position to the current location of the device.
mLastKnownLocation = task.getResult();
if (mLastKnownLocation != null) {
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
}
} else {
Log.d(TAG, "Current location is null. Using defaults.");
Log.e(TAG, "Exception: %s", task.getException());
gMap.moveCamera(CameraUpdateFactory
.newLatLngZoom(mDefaultLocation, DEFAULT_ZOOM));
gMap.getUiSettings().setMyLocationButtonEnabled(false);
}
}
});
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
private void updateLocationUI() {
if (gMap == null) {
return;
}
try {
if (mLocationPermissionGranted) {
gMap.setMyLocationEnabled(true);
gMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
gMap.setMyLocationEnabled(false);
gMap.getUiSettings().setMyLocationButtonEnabled(false);
mLastKnownLocation = null;
// getLocationPermission();
}
} catch (SecurityException e) {
Log.e("Exception: %s", e.getMessage());
}
}
}
<?php
$con=mysqli_connect("localhost","test","","student");
$st_id= strip_tags(trim($_GET["st_id"]));
$sql="SELECT * FROM Student where st_id= $st_id";
$result=mysqli_query($con,$sql);
$data=array();
while($row=mysqli_fetch_assoc($result)){
$data["data"][]=$row;
}
//header('Content-Type:Application/json');
echo json_encode($data);
?>
Answer
Solution:
Ok, you should declare this at the top of your activity :
private String st_id = "";
Then inonCreate()
, get your string from the intent like you did.
st_id = getIntent().getStringExtra("st_id");
And when calling method getMarkers(), make sure yourst_id
String is not empty, because php will send you the entire data base.
here :
final String url ="......../stedant/map.php?st_id=" + st_id;
tell me if that solved your pb;
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.