java - GPS 坐标未正确存储在从 android 到 php 的数据库中

标签 java android mysql coordinates

你知道为什么当我将它存储到我的数据库时我的坐标不断更改为 xxx.000000 吗?之前,它工作正常,但是当我继续测试它时,它没有正确保存我的坐标。

例如,我想存储 120.993235 和 14.612364,然后当它在数据库中时转换为 120.000000 和 14.000000。你们能帮帮我吗?这些是我的代码。

**

public class LocationGetter extends MapActivity implements LocationListener { // <1>
    private static final String TAG = "LocationActivity";
    LocationManager locationManager; // <2>
    Geocoder geocoder; // <3>
    TextView locationText;
    MapView map;
    MapController mapController; // <4>
    // ** This declarations was for passing of data to web service
    // Progress Dialog
    private ProgressDialog pDialog;
    // JSONParser Object creation
    JSONParser jsonParser = new JSONParser();
    // url to pass location to web
    // private static String url_create_product =
    // "http://student-thesis.netii.net/location_adding.php";
    private static String url_create_product = "http://10.0.2.2/TheCalling/location_adding.php";
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    //Latitude and Longitude
    public static double ILatitude;
    public static double ILongitude;
    // ** End
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps);
        locationText = (TextView) this.findViewById(R.id.lblLocationInfo);
        map = (MapView) this.findViewById(R.id.mapview);
        map.setBuiltInZoomControls(true);
        mapController = map.getController(); // <4>
        mapController.setZoom(19);
        locationManager = (LocationManager) this
                .getSystemService(LOCATION_SERVICE); // <2>
        geocoder = new Geocoder(this); // <3>
        Location location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER); // <5>
        if (location != null) {
            Log.d(TAG, location.toString());
            this.onLocationChanged(location); // <6>
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                60000, 5, this); // <7>
    }
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this); // <8>
    }
    @Override
    public void onLocationChanged(Location location) { // <9>
        Log.d(TAG, "onLocationChanged with location " + location.toString());
        String text = String.format(
                "Lat:\t %f\nLong:\t %f\nAlt:\t %f\nBearing:\t %f \n",
                location.getLatitude(), location.getLongitude(),
                location.getAltitude(), location.getBearing());
        this.locationText.setText(text);
        try {
            List<Address> addresses = geocoder.getFromLocation(
                    location.getLatitude(), location.getLongitude(), 10); // <10>
            for (Address address : addresses) {
                this.locationText.append(" " + address.getAddressLine(0));
            }
            int latitude = (int) (location.getLatitude());
            int longitude = (int) (location.getLongitude());

            int GLatitude = (int) (location.getLatitude() * 1000000);
            int GLongitude = (int)(location.getLongitude() * 1000000);

            GeoPoint point = new GeoPoint(GLatitude, GLongitude);
            mapController.animateTo(point); // <11>
            List<Overlay> mapOverlays = map.getOverlays();
            Drawable drawable = this.getResources().getDrawable(
                    R.drawable.reddot);
            AddItemizedOverlay itemizedOverlay = new AddItemizedOverlay(
                    drawable, this);
            OverlayItem overlayitem = new OverlayItem(point, "Hello",
                    "Sample Overlay item");
            itemizedOverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedOverlay);

            ILatitude = (double)latitude;
            ILongitude = (double)longitude;
            new phpconnect().execute();

        } catch (IOException e) {
            Log.e("LocateMe", "Could not get Geocoder data", e);
        }
    }
    @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
    }
    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
    class phpconnect extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... args) {
            String strLatitude = Double.toString(ILatitude);
            String strLongitude = Double.toString(ILongitude);
            // Building parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("latitude", strLatitude));
            params.add(new BasicNameValuePair("longitude", strLongitude));
            // 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());
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully updated
                } else {
                    // failed to create product
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
}

**

最佳答案

在这里您可以设置 后的所有值。归零。

    int latitude = (int) (location.getLatitude());
    int longitude = (int) (location.getLongitude());

还有这个:

    ILatitude = (double)latitude;
    ILongitude = (double)longitude;

不会返回归零值。 您可以尝试一下:

    ILatitude = location.getLatitude();
    ILongitude = location.getLongitude();

关于java - GPS 坐标未正确存储在从 android 到 php 的数据库中 ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067542/

相关文章:

java - 如何通过android中的代码关闭和重新启动模拟器?

java - 如何对没有返回值但写入 System.out 的方法进行单元测试

java - 我可以在 Android Studio 中使用 C# 吗?

android - 在 Android 中更新应用小部件中的 Intent

mysql - 涉及时区时如何与mysql进行时间比较?

java - 包 com.mysql.jdbc.exceptions.jdbc4 在 Mysql Connector/J 8 中不存在

java - OpenCV - 椭圆根本不显示

java - GetKey 方法在 dataSnapshot 中返回 null

php - 如何将 OOP 中的对象存储在 MySQL 等数据库中

php - 获取数组 - 在 CSV 中合并一些结果