Google maps mark with points from the database

1

I want to get the points, name and description plus 2 values to add to the snippet, but when I run the code I have the app does not fall, but it is black, then I leave my code

public class Marcadores {

    String ip, db, un, passwords;
    Connection con;
    PreparedStatement stmt;
    ResultSet rs;

    public void MarcadoreBdd(GoogleMap map) {
        ip = "mssql4.gear.host";
        db = "ciclomapp1";
        un = "ciclomapp1";
        passwords = "Mk36-9DX-580";

        String query = "select ru.nombre , ru.descripcion , avg(va.valoracion) ,re.tipo, ru.ubicacion  \n" +
                "from (Rutas ru inner join valorar va on ru.nombre=va.nombre_ruta) \n" +
                "inner join reporte re on re.nombre_ruta =Ru.nombre\n" +
                "group by ru.nombre,ru.descripcion , va.valoracion ,re.tipo, ru.ubicacion; ";
        try {
            con = connectionclass(un, passwords, db, ip);
            stmt = con.prepareStatement(query);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Ruta b = null;
                b.nombre = rs.getString("nombre");
                b.descripcion= rs.getString("descripcion");
                b.ubicacion= rs.getString("ubicacion");
                b.valoracion=rs.getString("valoracion").toString();
                b.reporte=rs.getString("reporte");
                String [] point=b.ubicacion.split(",");
                double latitude = Double.parseDouble(point[0]);
                double longitude = Double.parseDouble(point[1]);
                LatLng location = new LatLng(latitude, longitude);
                String valorar=String.valueOf(b.getValoracion());

                map.addMarker(new MarkerOptions().position(location).snippet(b.descripcion+ System.getProperty ("line.separator")+"Valoracion de la ruta"
                        +valorar+System.getProperty ("line.separator")+"Estado de la ruta: Posible"+b.reporte));
            }
        } catch (SQLException e) {

            e.printStackTrace();

        }
    }

        @SuppressLint("NewApi")
        public Connection connectionclass (String user, String password, String database, String server){
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            Connection connection = null;
            String ConnectionURL = null;
            try {
                Class.forName("net.sourceforge.jtds.jdbc.Driver");
                ConnectionURL = "jdbc:jtds:sqlserver://" + server + "/" + database + ";user=" + user + ";password=" + password + ";";
                connection = DriverManager.getConnection(ConnectionURL);
            } catch (SQLException se) {
                Log.e("error here 1 : ", se.getMessage());
            } catch (ClassNotFoundException e) {
                Log.e("error here 2 : ", e.getMessage());
            } catch (Exception e) {
                Log.e("error here 3 : ", e.getMessage());
            }
            return connection;
        }
}
    
asked by zhet 01.11.2016 в 22:07
source

2 answers

1

If your database is in the cloud, this is not the way you should do it.

In order to interact with the database you need to create a API where there is a web service that you return the information you need.

What the api does is:

  • You ask for information
  • The API receives that request and queries the database
  • The database returns the information
  • The API receives the information from the BD and returns it to you (to the mobile app)
  • When you have your API and your web services ready I suggest you use Retrofit is a bookstore that saves you a lot of work .. .

    When you have everything ready : An example of how you can receive the information from the database is through a Json (which Retrofit would already parse and you just have to use the info received)

    e.g.

    [{
        "nombre": "punto 1",
        "descripcion": "bla bla",
        "lat": "10.012",
        "lng": "-10202"
    }, {
        "nombre": "punto 2",
        "descripcion": "bla bla",
        "lat": "10.012",
        "lng": "-10202"
    }, {
        "nombre": "punto 3",
        "descripcion": "bla bla",
        "lat": "10.012",
        "lng": "-10202"
    }, {
        "nombre": "punto 4",
        "descripcion": "bla bla",
        "lat": "10.012",
        "lng": "-10202"
    }]
    

    The above is a JsonArray

    Now, what you should do next is to tell Retrofit that what you are going to receive is a JsonArray for which you must store it in a list.

    {
       "nombre": "punto 4",
       "descripcion": "bla bla",
       "lat": "10.012",
       "lng": "-10202"
    }
    

    This is an object of JsonArray therefore your list will be of objects

    Something like ArrayList<MiObjeto> miListaDeObjetos

    public class MiObjeto {
    String nombre;
    String descripcion;
    double lat;
    double lng;
    
    ...
    
    setters 
    getters
    }
    

    Since you have this, you only have to paint on the map with a foreach markers

    for (MiObjeto obj : miListaDeObjetos){
          MarkerOptions markerOptions = new MarkerOptions()
           .position(new LatLng(obj.getLat(), obj.getLng()))
           .title(obj.getNombre());
    
         googleMap.addMarker(markerOptions);
    }
    

    I hope it helps you. Using an API seems to me a best practice. Greetings

        
    answered by 02.11.2016 в 01:01
    0

    Robert is right, you should use good programming practices. When you create a mobile app, it is recommended to run database queries on a server, and not in the same application. Because you tend to duplicate code and your code will be not very readable, so it is recommended to mainly divide your system.

    You can create an API (REST API or RESTFUL API) in the language you want php, java, etc.

    When you make queries from the android app to the database you can use Retrofit like Robert or you can use Volley that is simpler link

    In addition, to perform your tests on the api, you can test the queries to your server using POSTMAN or another 'api client rest' link

    Finally check if your server has public access, that is if you can connect to it. If you have access to the bd or folders on the server.

    PS: when you publish do not show ip, name of bd, or less passwords :)

        
    answered by 02.11.2016 в 04:24