将当前位置保存到mysql

时间:2017-03-23 04:06:43

标签: php android mysql google-maps

我正在尝试将当前位置保存到我的sql。

我的代码来自MapsActivity页面(这是启动器活动后的主页面)

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener,
    LocationListener {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
Marker mCurrLocationMarker;
LocationRequest mLocationRequest;
Button send;
Button receive;
private static final String LOGIN_URL = "http://192.168.211.1/xyz/latlng.php";
public static final String KEY_MYNAME = "User";
public static final String LAT = "lat";
public static final String LANG = "lang";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SubmitForm();
    send = (Button)findViewById(R.id.send);
    receive = (Button)findViewById(R.id.receive);
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SubmitForm();
            Intent intent = new Intent(MapsActivity.this, send.class);
            startActivity(intent);
        }
    });
    receive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SubmitForm();
            Intent intent = new Intent(MapsActivity.this, receive.class);
            startActivity(intent);
        }
    });
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    }
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}
private void SubmitForm()
{
final Location location=null;
if(mLastLocation!=null) {
    Toast.makeText(MapsActivity.this, String.valueOf(location.getLatitude()), Toast.LENGTH_LONG).show();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, LOGIN_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (!response.equalsIgnoreCase("updated")) {
                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(MapsActivity.this, error.toString(), Toast.LENGTH_LONG).show();
                }
            }) {
        protected Map<String, String> getParams() {
            SharedPreferences sharedPreferences = MapsActivity.this.getSharedPreferences(Config.SHARED_PREF_NAME, Context.MODE_PRIVATE);
            String KEY_MYUSERNAME = sharedPreferences.getString(Config.EMAIL_SHARED_PREF, "Not Found");
            Map<String, String> params = new HashMap<String, String>();
            params.put(LAT, String.valueOf(location.getLatitude()));
            params.put(LANG, Double.toString(location.getLongitude()));
            params.put(KEY_MYNAME, KEY_MYUSERNAME);
            return params;
        }
    };
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}}

latlng.php如下所示:

<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
    $lat = $_POST['lat'];
    $lang = $_POST['lang'];
    $myusername = $_POST['User'];
    require_once('dbConnext.php');
    $sql = "UPDATE `registration` SET `latitude`='$lat', `longitude`='$lang' where `Username`='$myusername'";
    $result = mysqli_query($con,$sql);
    if(isset($result))
    {
        echo 'updated';
    }else{
        echo 'error';
    }
}?>

数据库如下所示:

Database image

有谁能告诉我要添加什么才能将当前位置保存到数据库?

SubmitForm()函数对我不起作用。

0 个答案:

没有答案