1

The application has a map with markers, the markers' visibiliy is set to false, i want the markers to show up when the device is at a certain range of any marker, as a result setting that marker to be visible. I assume it has something to do with the method location.distanceTo(anotherLocation) and then show location if distance to is less than a pre-defined distance in meters. But i am failing at making this work.
This is the method to add the markers onto the map from an arraylist.

        for(MyLatLngData location : locations){
         mMap.addMarker(new MarkerOptions()
                    .position(location.getLatLng())
                    .title(location.getTitle())
                    .visible(false));
        }

Here is how i store the data in arrays from a database using a MyLatLngData object.

    void storeDataInArrays() {
        Cursor cursor = databaseHelper.readAllData();
        if (cursor.getCount() == 0) {
            Toast.makeText(this, "No data", Toast.LENGTH_SHORT).show();
        } else {
            while (cursor.moveToNext()) {
                // remove all previous list adds.
                locations.add(new MyLatLngData(
                        cursor.getString(0),
                        cursor.getString(1),
                        cursor.getDouble(2),
                        cursor.getDouble(3)));
            }
        }
    }

How do i take account of all the locations using the distanceTo method and how do i set the first location to be the fusedLocationProvider's current location.

As an addition i would like the markers' states to be saved so that the ones that are set visible will stay visible.

Any thanks are well appreciated and i hope that someone could help with this as my programming skills are still being polished.

Edward Aarma
  • 305
  • 2
  • 8

1 Answers1

1

If you use location.distanceTo(anotherLocation) method you need to recalculate distance for every new coordinates of location. But instead of location.distanceTo(anotherLocation) you can determine lat/lon bounds (min_lat/min_lon - max_lat/max_lon) of area near current location and select from database rows with conditionб e.g.:

Cursor myCursor = db.rawQuery("SELECT * FROM markers WHERE lon >= min_lon AND lon <= max_lon AND lat >= min_lat AND lat <= max_lat", null);

For determine lat/lon bounds (min_lat/min_lon - max_lat/max_lon) of area you can use this answer of :

private LatLng getDestinationPoint(LatLng source, double brng, double dist) {
    dist = dist / 6371;
    brng = Math.toRadians(brng);

    double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
    double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
                            Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
    double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                    Math.cos(lat1),
                                    Math.cos(dist) - Math.sin(lat1) *
                                    Math.sin(lat2));
    if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
        return null;
    }
    return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
}

...

LatLng northEast = getDestinationPoint(location, 45, your_distance);
LatLng southWest = getDestinationPoint(location, 225, your_distance);

min_lat = southWest.latitude;
min_lon = southWest.longitude;

max_lat = northEast.latitude;
max_lon = northEast.longitude;
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • I understand this solution for the most part, but at the second part of the solution, there is getDestinationPoint(location, 45, your_distance); what are location and your_distance in this example? – Edward Aarma Nov 20 '20 at 01:24
  • @EdwardAarma `your_distance` is distance in meters to the North East and South East corners of square area within you need to get the markers. – Andrii Omelchenko Nov 20 '20 at 06:48
  • Thank you for the information, a different solution to what i had figured, though I am encountering a strange error where ``` String myQuery = "SELECT * FROM " + TABLE_NAME + "WHERE longitude <= min_lon AND longitude <= max_lon" + " AND latitude >= min_lat AND latitude <= max_lat"; ``` the first "<=" symbol is just an error, says GROUP, INDEXED, LIMIT, NOT, ORDER, WHERE or semicolon expected, got '<='. So far have not managed to make it work, but managed to find another way through using SphericalUtil.computeDistanceBetween method. Though thank you either ways. – Edward Aarma Nov 21 '20 at 03:07
  • @EdwardAarma Take a look at [this](https://stackoverflow.com/a/7649505/6950238). – Andrii Omelchenko Nov 21 '20 at 18:40