Search code examples
androidsqlitegoogle-maps-markersandroid-maps

android maps displayed multiple markers (LatLng from sqlite)


i use google maps with get (user) device location (GPS Location) in my android app, and insert into database (SQLite) latitude and longitude and adress !

now i want displayed multiple location with LatLng read from database ... no problem in create marker, but in marker info (country, city ...) only show last inserted location for all markers !

this my code :

private void displayMultiplePoint() {
        if (LOCATION_TABLE.size() > 0) {
            for (int i = 0; LOCATION_TABLE.size() > i; i++) {
                int id = LOCATION_TABLE.get(i).getId();
                lat = LOCATION_TABLE.get(i).getLatitude();
                lng = LOCATION_TABLE.get(i).getLongitude();
                place = LOCATION_TABLE.get(i).getPlace();
                rate = LOCATION_TABLE.get(i).getRate();
                drawMarker(new LatLng(lat, lng), "city", place, rate);
                displayToast(id + "" + place);
            }
        }
    }

    private void drawMarker(final LatLng point, final String city, final String place, final float rate) {
        mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker arg0) {
                return null;
            }

            @Override
            public View getInfoContents(Marker arg0) {
                View v = null;
                try {
                    v = getLayoutInflater().inflate(R.layout.custom_info_contents, null);
                    ImageView map_image = (ImageView) v.findViewById(R.id.maps_image);
                    map_image.setImageResource(R.drawable.runhd);
                    TextView city_txt = (TextView) v.findViewById(R.id.maps_city);
                    city_txt.setText(city);
                    TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
                    place_txt.setText(place);
                    RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
                    rate_bar.setRating(rate);
                } catch (Exception ev) {
                    System.out.print(ev.getMessage());
                }
                return v;
            }
        });
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(point);
        mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 6));
    }

i show toast form rowId from lcation table in databse, and displaled 3 row id : 1, 2, 3 but in marker info show last id (id no : 3)

this my fragment :

enter image description here

thank's


Solution

  • i Use Cursor through your database and get data rows (and show in Toast) but my code change to this :

    private void displayMultiplePoint() {
            Cursor cursor = DB_HELPER.LOCATION_MARKERS();
            if (cursor.moveToFirst()) {
                for (int i = 0; cursor.getCount() > i; i++) {
                    int id = cursor.getInt(cursor.getColumnIndex("id"));
                    double lat = cursor.getDouble(cursor.getColumnIndex("latitude"));
                    double lng = cursor.getDouble(cursor.getColumnIndex("longitude"));
                    String place = cursor.getString(cursor.getColumnIndex("place"));
                    float rate = cursor.getFloat(cursor.getColumnIndex("rate"));
                    displayToast(id + ", " + place + rate);
                    cursor.moveToNext();
                    drawMarker(new LatLng(lat, lng));
                }
                cursor.close();
            }
        }
    

    i get arg0.getId form : (m0, m1, m2 ...)

    public View getInfoContents(Marker arg0)
    

    (is unique for each marker) then select data by id where id = arg0