如何在定义的范围内显示标记?

k2arahey  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(320)

应用程序有一个带有标记的Map,标记的可见性设置为false,我希望当设备位于任何标记的特定范围时,标记显示出来,从而将该标记设置为可见。我假设它与location.distanceto(另一个位置)方法有关,如果distance to小于预定义的米距离,则显示位置。但我做不到这一点。
这是将标记从arraylist添加到Map上的方法。

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

下面是如何使用mylatlngdata对象将数据库中的数据存储在数组中。

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

如何使用distanceto方法考虑所有位置,以及如何将第一个位置设置为fusedlocationprovider的当前位置。
作为一个补充,我想保存标记的状态,以便那些设置为可见的将保持可见。
任何感谢都是非常感谢的,我希望有人能帮助我,因为我的编程技能仍在不断完善。

gcuhipw9

gcuhipw91#

如果你使用 location.distanceTo(anotherLocation) 方法,您需要为的每个新坐标重新计算距离 location . 但不是 location.distanceTo(anotherLocation) 您可以确定电流附近区域的纬度/经度界限(最小纬度/最小经度-最大纬度/最大经度) location 并从具有条件的数据库行中进行选择б 例如。: Cursor myCursor = db.rawQuery("SELECT * FROM markers WHERE lon >= min_lon AND lon <= max_lon AND lat >= min_lat AND lat <= max_lat", null); 对于确定区域的纬度/经度界限(最小纬度/最小经度-最大纬度/最大经度),您可以使用以下答案:

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;

相关问题