我想从服务器检索所有纬度和经度位置,该位置已经存储并显示在谷歌地图标记中
我的服务器文件:http://plantnow.net16.net/googlemaplocations.php
但是我只能在google地图中获得一个标记,即treeid = 2,其余位置在地图中没有显示任何标记。
我在下面的代码中出错了,请帮助我!
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");
List<Marker> markers = new ArrayList<Marker>();
// Check for error node in json
if (!error) {
JSONObject location = jObj.getJSONObject("tree");
for (int i = 0; i < 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);
}
我的PHP代码:
<?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 where `treecondition` IN ( 'Balanced', 'Healthy', 'Imbalance', 'Dangerous', 'Transplanted', 'Dieseased')")){
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();
}
答案 0 :(得分:0)
将所有对象添加到列表中,只调用json_encode()
一次。
$response = array();
while($tree = $stmt->fetch_assoc()) {
$treeItem = array();
$treeItem["treeid"] = $tree['treeid'];
$treeItem["treespecies"] = $tree['treespecies'];
$treeItem["treelatitude"] = $tree['treelatitude'];
$treeItem["treelongitude"] = $tree['treelongitude'];
$response[] = $treeItem;
// Might just be easier to do this instead
// $response[] = $tree;
}
echo json_encode($response);
然后在你的android代码中,迭代列表。
JSONArray jTreeList = new JSONArray(response);
for (int i = 0; i < jTreeList.length(); i++) {
JSONObject jTreeObj = jTreeList.getJSONObject(i);
String treeid = jTreeObj.getString("treeid");
// read rest of fields
// create marker, etc
}