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();
?>