Only Plotting Markers Within a Drawn Circle or Perimeter Google Maps v2 Android

会有一股神秘感。 提交于 2019-12-13 07:46:01

问题


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 co-ordinates fall within the circle drawn around the users current location, however I can't quite figure out how to do it. At the moment, I have written code that plots the users current location on the map along with the locations of all the entries stored on my database and also the circle around the current location (See picture below). Based on my picture below, I only want the three markers within the circle to be plotted.

Does anyone know if there are any ways of checking if the latlng co-ordinates stored in my database fall within the area of the circle? Or if not, could anyone suggest any alternatives that use a similar idea.

An alternative I was thinking of was using a square / rectangle rather than a circle so that I could simply compare the co-ordinates of my entries with bounds of the square / rectangle, however I don't really know how feasible that is seen as those shapes aren't supported by the Google Maps API. I also came across the LatLngBounds class which could be useful but I can't find any sample code using it. How I might do this either?


回答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

public static String getDistance(LatLng ll_source, LatLng ll_destination,
        int unit) {


    int Radius = 6371;// radius of earth in Km

    double 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 = new DecimalFormat("####");
    Integer kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    Integer meterInDec = Integer.valueOf(newFormat.format(meter));
    DecimalFormat df = new DecimalFormat("#.#");
    return df.format(valueResult);
}



回答2:


Ok, I've figured out a solution using the LatLngBounds class I referred to in my question. What it does is:

  1. Creates a new rectangular perimeter around the user's current latitude and longitude co-ordinates (this example is roughly one square kilometer).
  2. It then checks if the perimeter contains the co-ordinates stored in the database.
  3. If it does, the marker is plotted on the map.

    public void getNearbyMarkers(){
        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();
        }
    }
    


来源:https://stackoverflow.com/questions/15339661/only-plotting-markers-within-a-drawn-circle-or-perimeter-google-maps-v2-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!