-1

How can I get locations from a Mysql database and display them on a android app showing a marker on the map?

I have a custom Google map that currently gets the locations from an array or javascript variable named locations. I want the data to no longer be enter manually, but to be inserted dynamically from a database. How can I go about achieving this? I am using the Google map Api.

Thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

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";
jhayjhay
  • 78
  • 8