Php 使用数据库中的坐标在地图上添加标记,使用JSON检索数据

Php 使用数据库中的坐标在地图上添加标记,使用JSON检索数据,php,android,json,Php,Android,Json,我正在尝试开发一个应用程序,它可以在mapready上添加从数据库检索到坐标的标记。到目前为止,为了将数据发送到数据库(注册/登录/获取配置文件)等,我使用了JSON。现在,我尝试用同样的方法从数据库中获取纬度和经度,但调试器在调用Json方法后向我显示变量为Null。另外,我想问一些关于如何放置标记的问题?我的意思是,通过JSONObject获取坐标,它会给出表中的所有行吗?或者我必须使用其他方法获取所有行并为每个行添加标记吗?谢谢 AddMarker在onMapReady的末尾调用以获取坐标

我正在尝试开发一个应用程序,它可以在mapready上添加从数据库检索到坐标的标记。到目前为止,为了将数据发送到数据库(注册/登录/获取配置文件)等,我使用了
JSON
。现在,我尝试用同样的方法从数据库中获取纬度和经度,但调试器在调用Json方法后向我显示变量为Null。另外,我想问一些关于如何放置标记的问题?我的意思是,通过
JSONObject
获取坐标,它会给出表中的所有行吗?或者我必须使用其他方法获取所有行并为每个行添加标记吗?谢谢

AddMarker在onMapReady的末尾调用以获取坐标,这样我就可以添加一个标记:

 private void AdaugaMarker() {

    class AdaugaMarker extends AsyncTask<Void, Void, String> {

        @Override
        protected String doInBackground(Void... voids) {
            //creating request handler object
            RequestHandler requestHandler = new RequestHandler();

            //creating request parameters
            HashMap<String, String> params = new HashMap<>();
            params.put("problema", finalProblema);

            //returing the response
            return requestHandler.sendPostRequest(URLs.URL_GETALERTE, params);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            //displaying the progress bar while user registers on the server
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //hiding the progressbar after completion

            try {
                //converting response to json object
                JSONObject obj = new JSONObject(s);

                //if no error in response
                if (!obj.getBoolean("error")) {
                    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                    //getting the user from the response
                    JSONObject userJson = obj.getJSONObject("locatie");

                    latitudine_sql =Double.valueOf(userJson.getString("latitudine"));
                    longitudine_sql = Double.valueOf(userJson.getString("longitudine"));
                    tip_problema_sql = userJson.getString("tip_problema");
                } else {
                    Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    //executing the async task
    AdaugaMarker ru = new AdaugaMarker();
    ru.execute();
}
在.php的开头,我添加了
$response=array()

编辑: 添加了JSONArray:

//converting response to json object
                JSONObject obj = new JSONObject(s);

                //if no error in response
                if (!obj.getBoolean("error")) {
                    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                    JSONArray locatieArray = obj.getJSONArray("locatie");
                    for (int i = 0; i < locatieArray.length(); i++) {
                        JSONObject locatie = locatieArray.getJSONObject(i);
                        // check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method.
                        if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
                            latitudine_sql =Double.valueOf(locatie.getString("latitudine"));
                            longitudine_sql = Double.valueOf(locatie.getString("longitudine"));
                            addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
                        }
                        tip_problema_sql = locatie.getString("tip_problema");
                    }

                } else {
                    Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
//将响应转换为json对象
JSONObject obj=新的JSONObject;
//如果响应中没有错误
如果(!obj.getBoolean(“错误”)){
Toast.makeText(getApplicationContext(),obj.getString(“消息”),Toast.LENGTH_SHORT.show();
JSONArray locatiarray=obj.getJSONArray(“locatie”);
对于(int i=0;i
试试这种方法

//if no error in response
if (!obj.getBoolean("error")) {
    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

    //getting the user from the response
    JSONObject userJson = obj.getJSONObject("locatie");

    // check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method. 
    if(!userJson.isNull("latitudine") && !userJson.isNull("longitudine")) {
        latitudine_sql =Double.valueOf(userJson.getString("latitudine"));
        longitudine_sql = Double.valueOf(userJson.getString("longitudine"));

        addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
    }
    tip_problema_sql = userJson.getString("tip_problema");

} else {
    Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
更新:

对于多个标记

// get locatie as JsonArray
JSONArray locatieArray = obj.getJSONArray("locatie");
for (int i = 0; i < locatieArray.length(); i++) {
      JSONObject locatie = locatieArray.getJSONObject[i];

      // check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method. 
      if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
          latitudine_sql =Double.valueOf(userJson.getString("latitudine"));
          longitudine_sql = Double.valueOf(userJson.getString("longitudine"));

          addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
      }
      tip_problema_sql = userJson.getString("tip_problema");
}
//将位置作为JsonArray获取
JSONArray locatiarray=obj.getJSONArray(“locatie”);
对于(int i=0;i
试试这种方法

//if no error in response
if (!obj.getBoolean("error")) {
    Toast.makeText(getApplicationContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

    //getting the user from the response
    JSONObject userJson = obj.getJSONObject("locatie");

    // check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method. 
    if(!userJson.isNull("latitudine") && !userJson.isNull("longitudine")) {
        latitudine_sql =Double.valueOf(userJson.getString("latitudine"));
        longitudine_sql = Double.valueOf(userJson.getString("longitudine"));

        addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
    }
    tip_problema_sql = userJson.getString("tip_problema");

} else {
    Toast.makeText(getApplicationContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
}
更新:

对于多个标记

// get locatie as JsonArray
JSONArray locatieArray = obj.getJSONArray("locatie");
for (int i = 0; i < locatieArray.length(); i++) {
      JSONObject locatie = locatieArray.getJSONObject[i];

      // check latitudine and longitudine is not null and if not null then cast these values and call the addMarker() method. 
      if(!locatie.isNull("latitudine") && !locatie.isNull("longitudine")) {
          latitudine_sql =Double.valueOf(userJson.getString("latitudine"));
          longitudine_sql = Double.valueOf(userJson.getString("longitudine"));

          addMarker(latitudine_sql, longitudine_sql); // this method is implemented below
      }
      tip_problema_sql = userJson.getString("tip_problema");
}
//将位置作为JsonArray获取
JSONArray locatiarray=obj.getJSONArray(“locatie”);
对于(int i=0;i
您正在接收latitudine和Longtudine的JSONArray,但您正在作为JSONObject进行解析。 //首先创建这个类

public class Locatie {
private Double latitudine;

private String tip_problema;

private Double longitudine;

public Double getLatitudine() {
    return latitudine;
}

public void setLatitudine(Double latitudine) {
    this.latitudine = latitudine;
}

public String getTip_problema() {
    return tip_problema;
}

public void setTip_problema(String tip_problema) {
    this.tip_problema = tip_problema;
}

public Double getLongitudine() {
    return longitudine;
}

public void setLongitudine(Double longitudine) {
    this.longitudine = longitudine;
}

@Override
public String toString() {
    return "ClassPojo [latitudine = " + latitudine + ", tip_problema = " + tip_problema + ", longitudine = " + longitudine + "]";
}
}

if(!obj.getBoolean(“错误”)){
ArrayList locatieList=新的ArrayList();
Toast.makeText(getApplicationContext(),obj.getString(“消息”),Toast.LENGTH_SHORT.show();
//从响应中获取用户
JSONArray userLatLngArray=obj.getJSONArray(“locatie”);
对于(int i=0;i
您正在接收latitudine和Longtudine的JSONArray,但您正在作为JSONObject进行解析。 //首先创建这个类

public class Locatie {
private Double latitudine;

private String tip_problema;

private Double longitudine;

public Double getLatitudine() {
    return latitudine;
}

public void setLatitudine(Double latitudine) {
    this.latitudine = latitudine;
}

public String getTip_problema() {
    return tip_problema;
}

public void setTip_problema(String tip_problema) {
    this.tip_problema = tip_problema;
}

public Double getLongitudine() {
    return longitudine;
}

public void setLongitudine(Double longitudine) {
    this.longitudine = longitudine;
}

@Override
public String toString() {
    return "ClassPojo [latitudine = " + latitudine + ", tip_problema = " + tip_problema + ", longitudine = " + longitudine + "]";
}
}

if(!obj.getBoolean(“错误”)){
ArrayList locatieList=新的ArrayList();
Toast.makeText(getApplicationContext(),obj.getString(“消息”),Toast.LENGTH_SHORT.show();
//从响应中获取用户
JSONArray userLatLngArray=obj.getJSONArray(“locatie”);
对于(int i=0;i