3

All of markers shows up on maps based from data latitude and longitude from firebase. And i want to show markers on maps only inside of the circle, circle radius from current place is 10 kilometers.

This is how i show all markers from 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+"")));

This is how i show a circle around current place location :

LatLng latLng = new LatLng(latitude, longitude);
    mMap.addCircle(new CircleOptions()
                            .center(latLng)
                            .radius(10000)
                            .strokeWidth(0f)
                            .fillColor(0x550000FF));

Please ask me for more detail!

husen
  • 89
  • 2
  • 10
  • https://developers.google.com/maps/documentation/android-api/marker – Tim Biegeleisen Apr 15 '18 at 07:27
  • Use GeoFire (firebase utility) and perform GeoQuery using location as center point and radius of choice. All keyExited callbacks result in marker not visible from map and all those keyEntered made visible. https://github.com/firebase/geofire-java –  Apr 15 '18 at 11:51

2 Answers2

3

Use this formula to find distance between two latitude and longitude this function will return distance in meter

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

you can implement the logical part associated with your problem easily

Rishabh Dhiman
  • 159
  • 2
  • 9
0

When you add markers to the map, store objects inside an array. Then use computeDistanceBetween to calculate distance and setMap(null) to hide away markers.

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);
    }
});
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69