如何从Mysql数据库获取位置并将其显示在在地图上显示标记的android应用中?
我有一个自定义的Google地图,当前可从数组或名为locations的javascript变量中获取位置。我希望不再手动输入数据,而是从数据库动态插入数据。我该如何实现呢?我正在使用Google map Api。
预先感谢
答案 0 :(得分:0)
inside your MapActivity instanciate LocationListener.
under onLocationChange(Location location) insert your method to insert data in mySQL.
everytime you request for new location it will automatically save the location to your database
public void Connected(){
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(CONN, USER, PASSWORD);
System.out.println("CONNECTION ZERO::::"+connection);
} catch (SQLException e) {
e.printStackTrace();
// Toast.makeText(this, "asdasd", Toast.LENGTH_SHORT).show();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void getLocation() {
locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (isGpsEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 25000, 0, MainActivity.this);
if(locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
}
if(location == null){
if(isNetworkEnabled){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,25000 , 0, ll);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
}
if(location == null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
if(location != null){
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,25000 , 0, ll);
}
}
LocationListener ll = new LocationListener();
@Override
public void onLocationChanged(Location location) {
System.out.println(location.getLatitude()+",:::::::::"+location.getLongitude());
Constants.LONGITUDE = location.getLongitude();
Constants.LATITUDE = location.getLatitude();
Toast.makeText(this, ":::::::::::::::::::::::::onchangelocation::::"+Constants.LATITUDE+","+Constants.LONGITUDE+",\n"+location.getProvider(), Toast.LENGTH_LONG).show();
try {
// Connected();
String query = " insert into myusers_tbl (latitude, longitude)"
+ " values (?, ?)";
preparedStatement.setDouble(5, Constants.LATITUDE);
preparedStatement.setDouble(6, Constants.LONGITUDE);
System.out.println("prepared statement"+preparedStatement);
System.out.println("::::"+preparedStatement .executeUpdate());
connection.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("::::::::"+e);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
private static final String USER = "username"; private static final String PASSWORD = "password"; private static final String CONN = "jdbc:mysql://localhost:3306/database";