Google Places Api sort by distance

本秂侑毒 提交于 2019-11-28 08:46:22

Maybe you've already solved your problem, but I hope this can help (Focus on what is in bold):

radius - Defines the distance (in meters) Within Which to return Place results. The maximum allowed is 50 000 meters radius. Note That radius must not be included if rankby = distance (Described below under Optional parameters) is specified.

rankby — Specifies the order in which results are listed. Possible values are:

prominence (default). This option sorts results based on their importance. Ranking will favor prominent places within the specified area. Prominence can be affected by a Place's ranking in Google's index, the number of check-ins from your application, global popularity, and other factors.

distance. This option sorts results in ascending order by their distance from the specified location. When distance is specified, one or more of keyword, name, or types is required.

according: https://developers.google.com/places/documentation/search?hl=en

I have understood according to google documentation that you can not simultaneously send arguments "rankby" and "radius", you must use only one of them at the same time, this way you will get the results sorted by distance.

test the request doing this:

resourceURI = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?
location="+myLocation.latitude+","+myLocation.longitude+"&rankBy=distance
&types="+ URLEncoder.encode (types, "UTF-8") + "&sensor = true&
key = GOOGLE_MAPS_KEY";

to see how you do, good luck!

Hope this code will work..

    private ArrayList<CLPlaceDTO> sortLocation(LatLng currentLatLng, ArrayList<?> alLocationDTO)
    {
        ArrayList<CLPlaceDTO> clPlaceDTOArrayList;
        ArrayList<CLPlaceDTO> clPlaceDTOArrayList1 = new ArrayList<>();
        double dCurrentLat = currentLatLng.latitude;
        double dCurrentLong = currentLatLng.longitude;

        Iterator<?> clIterator = alLocationDTO.iterator();
        while (clIterator.hasNext())
        {
            clIterator.next();
            clPlaceDTOArrayList = new ArrayList<>();
            for (int j = 0; j < alLocationDTO.size(); j++)
            {
                CLLocationDTO clLocationDTO = (CLLocationDTO) alLocationDTO.get(j);
                double dLat = clLocationDTO.getLatitude().doubleValue();
                double dLng = clLocationDTO.getLongitude().doubleValue();
                LatLng clNewLatLng = new LatLng(dLat, dLng);
                double dDistance = getDistance(dCurrentLat, dCurrentLong, dLat, dLng);
                CLPlaceDTO clPlaceDTO = new CLPlaceDTO(clLocationDTO.getAccountName(), clNewLatLng, dDistance);
                clPlaceDTOArrayList.add(clPlaceDTO);
            }
            Collections.sort(clPlaceDTOArrayList, new CLSortPlaces(currentLatLng));
            dCurrentLat = clPlaceDTOArrayList.get(0).getLatlng().latitude;
            dCurrentLong = clPlaceDTOArrayList.get(0).getLatlng().longitude;
            clPlaceDTOArrayList1.add(clPlaceDTOArrayList.get(0));
            clIterator.remove();
        }

        return clPlaceDTOArrayList1;
    }

     public static double getDistance(double dbFromLatitude,double dbFromLongitude,double dbToLatitude,double dbToLongitude)
    {
          double dbRadiusMeters = EARTH_RADIUS * 1000 ; // Earth’s mean radius in meter
          double dbLatitudeDiff = Math.toRadians(dbToLatitude - dbFromLatitude);
          double dbLongitudeDiff = Math.toRadians(dbToLongitude - dbFromLongitude);

          double a = Math.sin(dbLatitudeDiff / 2) * Math.sin(dbLatitudeDiff / 2) +
                                    Math.cos(Math.toRadians(dbFromLatitude)) * Math.cos(Math.toRadians(dbToLatitude)) *
                                    Math.sin(dbLongitudeDiff / 2) * Math.sin(dbLongitudeDiff / 2);

          double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
          double d = dbRadiusMeters * c;
          return d; // returns the distance in meter
    }

    public class CLSortPlaces implements Comparator<CLPlaceDTO>
    {
        private LatLng currentLoc;

        CLSortPlaces(LatLng current)
        {
            currentLoc = current;
        }

        @Override
        public int compare(final CLPlaceDTO place1, final CLPlaceDTO place2)
        {

            return (int) (place1.dDistance - place2.dDistance);
        }
    }



     public class CLPlaceDTO
{
    public LatLng latlng;
    public String sNameOfLocation;
    public double dDistance;

    public CLPlaceDTO(String sNameOfLocation, LatLng latlng,double dDistance)
    {
        this.sNameOfLocation = sNameOfLocation;
        this.latlng = latlng;
        this.dDistance=dDistance;
    }
    public CLPlaceDTO(String sNameOfLocation, LatLng latlng)
    {
        this.sNameOfLocation = sNameOfLocation;
        this.latlng = latlng;
        this.dDistance=dDistance;
    }}

It's quite possible that the places that you're thinking are closer than the places that are being returned are not true Google places.

Check out this FAQ for getting your business on Google maps: https://support.google.com/places/answer/145585?hl=en .

A test for this could be to attempt to register the business you think is closer with Google places https://www.google.com/local/business/add?hl=en&migratedId=04614545856067425787 and see if it allows you. If you can, this would mean that they are not a true Google place and would not show up in this result set.

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