如何在地图上只显示半径(圆形)内的标记?

3

所有的标记都显示在基于Firebase数据纬度和经度的地图上。我想仅在圆内的地图上显示标记,圆的半径为当前位置的10公里。

以下是我如何从Firebase中显示所有标记的方法

 LatLng newlocation = new LatLng(dataSnapshot.child("latitude").getValue(Double.class),dataSnapshot.child("longitude").getValue(Double.class));
      nama = new String(dataSnapshot.child("nama").getValue(String.class));
 googleMap.addMarker(new MarkerOptions().position(newlocation).title(nama+"")));

这是我展示当前位置周围圆圈的方法:
LatLng latLng = new LatLng(latitude, longitude);
    mMap.addCircle(new CircleOptions()
                            .center(latLng)
                            .radius(10000)
                            .strokeWidth(0f)
                            .fillColor(0x550000FF));

请向我询问更多细节!


https://developers.google.com/maps/documentation/android-api/marker - Tim Biegeleisen
使用GeoFire(Firebase实用程序)并使用位置作为中心点和选择的半径执行GeoQuery。所有keyExited回调都会导致地图上不可见的标记,而所有keyEntered则会变得可见。https://github.com/firebase/geofire-java - user2711811
2个回答

3
使用以下公式来计算两个经纬度之间的距离,该函数将返回以米为单位的距离。
 double getDistance(double LAT1, double LONG1, double LAT2, double LONG2) {
    double distance = 2 * 6371000 * Math.asin(Math.sqrt(Math.pow((Math.sin((LAT2 * (3.14159 / 180) - LAT1 * (3.14159 / 180)) / 2)), 2) + Math.cos(LAT2 * (3.14159 / 180)) * Math.cos(LAT1 * (3.14159 / 180)) * Math.sin(Math.pow(((LONG2 * (3.14159 / 180) - LONG1 * (3.14159 / 180)) / 2), 2))));
    return distance;
}

您可以轻松实现与您的问题相关联的逻辑部分


0
当您向地图添加标记时,请将对象存储在数组中。然后使用computeDistanceBetween计算距离,使用setMap(null)隐藏标记。
var markers;
// adding...
var centerLatlng = new google.maps.LatLng(-25.363882,131.044922);

markers.each(function(){
    var distance = google.maps.geometry.spherical.computeDistanceBetween (this.getPosition(), centerLatLng);
    if(distance > 10000) {
        this.setMap(null);
    }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接