问:我通过排球库使用sqlite数据库连接在“片段”中使用Google Map。
我有4个位置,但我的地图仅显示一个标记。我不知道哪里出了错。
我需要在哪里更改代码以获取所有4个位置标记?
我的PHP文件:
<?php
define('&#95;&#95; ROOT&#95;&#95;',dirname(dirname(&#95;&#95; FILE&#95;&#95;)));;
require&#95once(&#95;&#95; ROOT&#95;&#95;。'/ public&#95; html / Config.php');
//连接到mysql数据库
$ mysqli =新的mysqli(DB&#95; HOST,DB&#95; USER,DB&#95; PASSWORD,DB&#95; DATABASE);
// json响应数组
$ response = array(“ error” => FALSE);
//获取Google Map Marker的树详细信息
if($ stmt = $ mysqli-> query(“ SELECT * FROM tree”)){
如果($ stmt-> num&#95; rows){
while($ tree = $ stmt-> fetch&#95; assoc()){
$ response [“错误”] =否;
$ 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>';
}
}其他{
//找不到具有凭据的用户
$ response [“ error”] = TRUE;
“; $ response [” error&#95; msg“] =”树列表视图凭据错误。请重试!“;
echo json&#95; encode($ response);
}
$ mysqli-> close();
}
PHP文件的输出:http://plantnow.net16.net/googlemaplocations.php
{“错误”:false,“树”:{“ treeid”:“ 2”,“ treespecies”:“ Hatti”,“ treelatitude”:“ 5.5”,“ treelongitude”:“ 5.5”}}
{“错误”:false,“树”:{“ treeid”:“ 1”,“ treespecies”:“ Kattoda”,“ treelatitude”:“ 15.4030008”,“ treelongitude”:“ 75.0770729”}}
{“错误”:false,“树”:{“ treeid”:“ 66”,“ treespecies”:“ Sampige”,“ treelatitude”:“ 15.4584362”,“ treelongitude”:“ 74.992202”}}
{“错误”:false,“树”:{“ treeid”:“ 99”,“ treespecies”:“ Halasu”,“ treelatitude”:“ 0”,“ treelongitude”:“ 0”}}
我的Fragmentactivity.java
btMap.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
字符串搜索= String.valueOf(inputSearch.getSelectedItem())。toString();
mMap = mMapView.getMap();
mMap.clear();
// getSelectedLocations(search);
Toast.makeText(getActivity()。getApplicationContext(),“ +搜索+树的位置”,Toast.LENGTH&#95; LONG).show();
如果(search.equals(“所有位置”)){
双纬= 0;
双经= 0;
字符串标记;
getLocations();
Toast.makeText(getActivity()。getApplicationContext(),“所有树的位置”,Toast.LENGTH&#95; LONG).show();
}其他{
Toast.makeText(getActivity()。getApplicationContext(),“位置不可用”,Toast.LENGTH&#95; LONG).show();
}
}
});
返回v;
}
私人无效getLocations(){
//用于取消请求的标签
字符串标签&#95; string&#95; req =“ req&#95; location”;
// pDialog.setMessage(“正在登录...”);
// showDialog();
StringRequest strReq =新的StringRequest(Request.Method.POST,
AppConfig.URL&#95; MAPLOCATION,新的Response.Listener <String>(){
@Override
public void onResponse(String response){
Log.d(TAG,“位置响应:” + response.toString());
hideDialog();
尝试{
JSONObject jObj =新的JSONObject(response);
布尔错误= jObj.getBoolean(“错误”);
//检查json中的错误节点
如果(!错误){
for(int tree = 0; tree <jObj.length(); tree ++){
JSONObject位置= jObj.getJSONObject(“ tree”);
字符串treeid = location.getString(“ treeid”);
字符串treespecies = location.getString(“ treespecies”);
Double treelatitude = location.getDouble(“ treelatitude”);
Double treelongitude = location.getDouble(“ treelongitude”);
LatLng latlng =新的LatLng(treelatitude,treelongitude);
mMap.addMarker(new MarkerOptions()。title(treeid)
.position(new LatLng(treelatitude,treelongitude))
);
//将CameraPosition移动到最后单击的位置
mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
//单击在地图上的最后位置设置缩放级别
mMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(4));
}
//启动主要活动
}其他{
//登录错误。取得错误讯息
字符串errorMsg = jObj.getString(“ error&#95; msg”);
Toast.makeText(getActivity()。getApplicationContext(),
errorMsg,Toast.LENGTH&#95; LONG).show();
}
} catch(JSONException e){
// JSON错误
e.printStackTrace();
Toast.makeText(getActivity()。getApplicationContext(),“ Json错误:” + e.getMessage(),Toast.LENGTH&#95; LONG).show();
}
}
},新的Response.ErrorListener(){
@Override
公共无效onErrorResponse(VolleyError错误){
Log.e(TAG,“登录错误:” + error.getMessage());
Toast.makeText(getActivity()。getApplicationContext(),
error.getMessage(),Toast.LENGTH&#95; LONG).show();
hideDialog();
}
}){
};
//将请求添加到请求队列
AppController.getInstance()。addToRequestQueue(strReq,tag&#95; string&#95; req);
}
答:简单来说,请检查json是否无效。这样就可以解决了。