-1

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

ghasem deh
  • 698
  • 1
  • 10
  • 26

3 Answers3

1

I have many solution for your case But at first :

Your fall with setInfoWindowAdapter method it's just invoked one time, So after you iterated your database items and passing info through drawMarker it's just shown the last modified (saved) data from the variables, So i suggest to move it in your for loop (I know it's not a perfect solution) :

 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);
            ..........
             @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;
            ......
        }

2nd Solution Using Cursor through your database and use it anywhere (This is will be awesome).

3rd Using Clustering Algorithm in google_maps-utils-example.

  • about this "Solution Using Cursor through your database and use it anywhere (This is will be awesome)." You have an example – ghasem deh Jun 27 '17 at 10:32
  • @ghasemdeh super easy just implement it like docs, and invoke any data you need, with out for loop. –  Jun 27 '17 at 15:49
  • 1
    please go through [this](https://stackoverflow.com/documentation/android/871/sqlite/5128/reading-data-from-a-cursor) –  Jun 27 '17 at 15:55
1

The info window is being shown for only last marker because setInfoWindowAdapter() sets info window for the entire map. Inside setInfoWindowAdapter() you need to associate marker argument with corresponding data.

  1. You need to maintain a marker to data map.

    Map<Marker, Place> markerToPlaceMap = new HashMap<>();
    

    where, Place is a class to hold city, place, and rating.

    class Place {
        public String city, place;
        public float rating;
    }
    

    Note: Please change members to private and implement getters and setters as per your suitability.

  2. Next, Your drawMarker() will change as follows. It needs to add the marker and it's related place to markerToPlace map.

    private void drawMarker(final LatLng point, final String city, final String place, final float rate) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(point);
        Marker marker = mMap.addMarker(markerOptions);
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 6));
        markerToPlaceMap.put(marker, new Place(city, place, rating));
    }
    
  3. Finally, you will override GoogleMap.setInfoWindowAdapter() and access Place related to a marker for setting corresponding info contents.

    mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
    
        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }
    
        @Override
        public View getInfoContents(Marker marker) {
            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(markerToPlace.get(marker).city); // <- Check how corresponding place for a marker is fetched and used
    
                TextView place_txt = (TextView) v.findViewById(R.id.maps_place);
                place_txt.setText(markerToPlace.get(marker).place);
    
                RatingBar rate_bar = (RatingBar) v.findViewById(R.id.exercise_display_rate);
                rate_bar.setRating(markerToPlace.get(marker).rate);
    
            } catch (Exception ev) {
                System.out.print(ev.getMessage());
            }
            return v;
        }
    });
    
BhalchandraSW
  • 714
  • 5
  • 13
0

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

ghasem deh
  • 698
  • 1
  • 10
  • 26