【发布时间】:2020-04-26 16:16:50
【问题描述】:
我使用 Google 地图来显示特定位置。 位置数据来自我的数据库,并且已经在数据库中可用。 我数据库中的每个字段都有自己的位置,如纬度和经度。
例如
学生 1 - 座位号 - 学期 - 纬度 - 经度
学生 1 - 3 - 2 - 33.8523341 - 151.2106085
这样……
问题是 Volley 连接一次从数据库中获取所有学生的位置数据。这是错误的,我应该只获取所选学生的数据(我使用条件句 (Where) 到 Volley 的 url 和 php 文件来获取具体数据。但我不知道为什么它仍然不起作用)。
如下图所示,我在数据库中有两个学生,当我订购一个学生时,他们的位置点同时出现。
你可以看到这张来自数据库的图片我现在必须学生:
如果有人知道解决方案,请帮助我,我需要根据数据库中的数据为每个学生显示不同的位置。
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);
?>
【问题讨论】:
-
相机不去选择学生的位置,而是去数据库中第一个学生的位置或标记
-
(您的 for 循环执行速度非常快, gmap.animateCamera() 无法跟上步伐,只会显示您的 LAST 学生)如果这是问题,我该如何解决,对不起新开发者我什么都不知道
-
现在的问题是 volley 代码一次从数据库中获取所有学生的位置数据。这是错误的,我应该只获取所选学生的数据(我使用条件句(Where)到 Volley 的 url和php文件来获取特定的数据。但我不知道它不起作用)。
-
我更新了我的问题以使其更清晰
-
您应该使用 mMap.moveCamera(CameraUpdateFactory.newLatLngZoom("location",12f)) 进行缩放