0

我在向 mysql 插入数据(纬度和经度)时遇到问题。当我第一次运行应用程序时,插入了纬度和经度,但是当我更新位置(纬度和经度)并再次插入数据时,只有经度发生变化,纬度保持与之前插入的数据相同的值。我完全困惑为什么它不起作用。如果有人能帮我解决这个问题,我会很高兴。

这是我的代码

    package com.android.tyegah;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;


     import android.app.Activity;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class TyegahActivity extends Activity {
    private LocationManager locationManager;
private Location latestLocation;
private double lat;
private double longi;
private Button updateButton;
private TextView locUpdate;
public String url = "http://servername/file.php";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,onLocationChange);

    locUpdate = (TextView)findViewById(R.id.locUpdate);
    updateButton = (Button)findViewById(R.id.updateButton);
    updateButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,onLocationChange);
            if (latestLocation.getLatitude() != lat && latestLocation.getLongitude() != longi ) {
                lat = latestLocation.getLatitude();
                longi = latestLocation.getLongitude();  
                }
            String latitude = Double.toString(lat);
            String longitude = Double.toString(longi);
            url  += "?latitude=" + latitude + "&longitude=" + longitude;
            locUpdate.setText(latitude + "," + longitude);
            getRequest(url);
            Toast.makeText(getBaseContext(),
                    "Location updated : Lat: " + latestLocation.getLatitude() +
                    " Lng: " + latestLocation.getLongitude(),
                    Toast.LENGTH_LONG).show();
            }
    }); 

 }


public void getRequest(String Url) {
    // TODO Auto-generated method stub
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet(url);
    try {
        HttpResponse response = client.execute(request);

    } catch (Exception ex) {
        locUpdate.setText(ex.toString());
    }
}

LocationListener onLocationChange = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        latestLocation = location;
        if (latestLocation.getLatitude() != lat && latestLocation.getLongitude() != longi ) {
        Toast.makeText(getBaseContext(),
                "Location updated : Lat: " + latestLocation.getLatitude() +
                " Lng: " + latestLocation.getLongitude(),
                Toast.LENGTH_LONG).show();
        }
    }
};

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, onLocationChange);
}

}

这是 php 文件:

<?php 
mysql_connect("localhost","root",""); 
mysql_select_db("php"); 
$longitude = $_GET['longitude'];
$latitude = $_GET['latitude'];
    if($latitude && $longitude){
      $string= "insert into table (latitude, longitude)values('".$latitude."','".$longitude."')";
       mysql_query($string);
       }

mysql_close();
?>
4

2 回答 2

0

您正在更新 onCreate() 方法的 url,请检查活动生命周期 onCreate() 方法将在创建活动或重新创建活动时第一次调用。我建议您在为服务器请求创建 url 时使用回调机制来获取位置数据。

于 2012-05-08T12:32:23.957 回答
0

只需更改两行并尝试

在android中你写过

if (latestLocation.getLatitude() != lat && latestLocation.getLongitude() != longi )

而是写

if (latestLocation.getLatitude() != lat || latestLocation.getLongitude() != longi )

和在 php

你已经推杆了

$string= "insert into table (latitude, longitude)values('".$latitude."','".$longitude."')";

而是把

$string= "insert into table (latitude, longitude) values('".$latitude."','".$longitude."')";

试试看,我认为它会工作

于 2012-05-08T12:27:14.533 回答