Only Plotting Markers Within A Drawn Circle Or Perimeter Google Maps V2 Android
Basically the title says it all. Rather than plotting every single entry in my database on a map as I do at the moment, I want to query the database and only plot the entries whose
Solution 1:
I believe the circle has a fixed Radius and a center point.
So, go with this method to get the distance between the center and some LatLng and set a condition
distance <= radius
publicstatic String getDistance(LatLng ll_source, LatLng ll_destination,
int unit){
int Radius = 6371;// radius of earth in Kmdouble lat1 = ll_source.latitude;
double lat2 = ll_destination.latitude;
double lon1 = ll_source.longitude;
double lon2 = ll_destination.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = newDecimalFormat("####");
Integer kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
Integer meterInDec = Integer.valueOf(newFormat.format(meter));
DecimalFormat df = newDecimalFormat("#.#");
return df.format(valueResult);
}
Solution 2:
Ok, I've figured out a solution using the LatLngBounds class I referred to in my question. What it does is:
- Creates a new rectangular perimeter around the user's current latitude and longitude co-ordinates (this example is roughly one square kilometer).
- It then checks if the perimeter contains the co-ordinates stored in the database.
If it does, the marker is plotted on the map.
publicvoidgetNearbyMarkers(){ LatLngBounds perimeter = new LatLngBounds(new LatLng(currentLat - 0.004, currentLon - 0.004), new LatLng(currentLat + 0.004, currentLon + 0.004)); if (perimeter.contains(LatlongFromDatabase)) { //Plot Marker on Map } else { Toast.makeText(getApplicationContext(), "Co-ordinates not in perimeter!", 8).show(); } }
Post a Comment for "Only Plotting Markers Within A Drawn Circle Or Perimeter Google Maps V2 Android"