【问题标题】:How to show markers only inside of radius (circle) on maps?如何仅在地图上的半径(圆圈)内显示标记?
【发布时间】:2018-09-25 03:18:57
【问题描述】:

所有标记都显示在基于 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));

详情请咨询我!

【问题讨论】:

标签: java android google-maps firebase


【解决方案1】:

使用此公式查找两个纬度和经度之间的距离此函数将返回以米为单位的距离

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

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

【讨论】:

    【解决方案2】:

    向地图添加标记时,将对象存储在数组中。然后使用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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2021-05-14
      • 2014-01-16
      • 2015-11-17
      • 2015-05-29
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      相关资源
      最近更新 更多