我一直试图将地图坐标传递给MySQL数据库,但我不知道如何传递我拥有的代码的数据。我昨晚尝试了,但它显示了许多错误....有谁可以帮助我如何解决问题...
这是我的MapActivity代码....
如果有人能帮助我,我会很感激吗?我想使用php脚本连接到MySQL,但我不介意任何人都可以提供更简单的连接数据库的解决方案
由于
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class LocationActivity extends FragmentActivity implements LocationListener {
GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//show error dialog if GooglePlayServices not available
if (!isGooglePlayServicesAvailable()) {
finish();
}
setContentView(R.layout.activity_location);
SupportMapFragment supportMapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.googleMap);
googleMap = supportMapFragment.getMap();
googleMap.setMyLocationEnabled(true);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
onLocationChanged(location);
}
locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
}
@Override
public void onLocationChanged(Location location) {
TextView locationTv = (TextView) findViewById(R.id.latlongLocation);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(latLng));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
locationTv.setText("Latitude:" + latitude + ", Longitude:" + longitude);
}
@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
}
private boolean isGooglePlayServicesAvailable() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (ConnectionResult.SUCCESS == status) {
return true;
} else {
GooglePlayServicesUtil.getErrorDialog(status, this, 0).show();
return false;
}
}
}
创建表格
<?php
//this opens the connection to the database you need to change this for your database
$con = mysqli_connect("http://localhost/phpmyadmin","root","password", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql = "CREATE TABLE data
(
long varchar(30),
lat varchar(30)
)";
// Execute query
mysqli_query($con, $sql);
mysqli_close($con);
?>
的GetList
<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
// This opens the connection to the database you need to change this for your database.
$con = mysqli_connect("http://localhost/phpmyadmin","root","password", "root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Retrieve all the data from the "data" table.
$result = mysqli_query($con, "SELECT * FROM data")
or die(mysqli_error());
// Initialise contacts array to hold our data.
$data = array();
// Add each row to contacts array.
while($row = mysqli_fetch_assoc($result)){
$data[] = $row;
}
// Pring data as json string.
print(json_encode($data));
// Close DB connection.
mysqli_close($con);
?>
插入
<?php
//this opens the connection to the database you need to change this for your database
$con = mysqli_connect("http://localhost/phpmyadmin","root","password", "root");
if (!$con)
{
die('Could not connect: ' . mysqli_error());
}
mysqli_query($con, "INSERT INTO data (long, lat)
VALUES ('".$_POST['Description']."', '".$_POST['long,lat']."')");
mysqli_close($con);
?>