【问题标题】:How can I store gps coordinates in mysql database?如何在 mysql 数据库中存储 gps 坐标?
【发布时间】:2013-01-21 08:17:45
【问题描述】:

我目前有一个带有 php 和 mysql 的 android 应用程序,但我的问题是,当我的代码传递给 php 并将其存储在 mysql 中时,它总是抛出 0.000000 经度和纬度。

这是我的代码

**

class myLocationlistener  implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            if (location != null) {
                double pLong = location.getLongitude();
                double pLat = location.getLatitude();
                textLat.setText(Double.toString(pLat));
                textLong.setText(Double.toString(pLong));
            }
        }
        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
        }
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
        }
    }

    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Sample.this);
            pDialog.setMessage("Sending Location");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String latitude = textLong.getText().toString();
            String longitude = textLat.getText().toString();

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("latitude", latitude));
            params.add(new BasicNameValuePair("longitude", longitude));

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);

            // check log cat fro response
            Log.d("Create Response", json.toString());

            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // successfully created product
                    Intent i = new Intent(getApplicationContext(), Echos.class);
                    startActivity(i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

**

这是用于 php 的

**

if ($_POST['latitude'] && $_POST['longitude'] != null) {
    $db_connect = mysql_connect($db_host, $db_user, $db_password) or die(mysql_error());
    $db = mysql_select_db($db_name);
    $latitude = $_POST['latitude'];
    $longitude = $_POST['longitude'];

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO gpslocation(latitude, longitude) VALUES('latitude', 'longitude')");


    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

**

如果有人对此有所了解,请在这里回答,或者告诉我我是否误导了我的问题。

【问题讨论】:

  • 经纬度字段的类型是什么?
  • 纬度为十进制(7, 5),经度为十进制(8, 5)
  • 您正在将字符串插入十进制字段(您忘记了纬度和经度的 $ 变量符号)

标签: java php mysql


【解决方案1】:

问题似乎出在您的 PHP 代码上。

$result = mysql_query("INSERT INTO gpslocation(latitude, longitude) VALUES('$latitude', '$longitude')");

把上面那行改成$符号看看。

最好将 mysql_execute 与准备好的语句一起使用。见this帖子

【讨论】:

  • 非常感谢你们,非常完美。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多