Android 谷歌将Api按距离排序

Android 谷歌将Api按距离排序,android,google-api,google-places-api,Android,Google Api,Google Places Api,目前正在开发一个Android应用程序,返回用户当前位置最近的20个位置 googleplacesapi正在返回约20个靠近用户位置的位置,但不是按距离排序的最近的20个位置 看一眼照片,我看不出有什么不正确的地方 GetPlaces.java String types = "accounting|airport|amusement_park|aquarium|art_gallery|atm|bakery|bank|bar|beauty_salon|bicycle_store|book_stor

目前正在开发一个
Android
应用程序,返回用户当前位置最近的20个位置

googleplacesapi
正在返回约20个靠近用户位置的位置,但不是按距离排序的最近的20个位置

看一眼照片,我看不出有什么不正确的地方

GetPlaces.java

String types = "accounting|airport|amusement_park|aquarium|art_gallery|atm|bakery|bank|bar|beauty_salon|bicycle_store|book_store|bowling_alley|bus_station|cafe|campground|car_dealer|car_rental|car_repair|car_wash|casino|cemetery|church|city_hall|clothing_store|convenience_store|courthouse|dentist|department_store|doctor|electrician|electronics_store|embassy|establishment|finance|fire_station|florist|food|funeral_home|furniture_store|gas_station|general_contractor|grocery_or_supermarket|gym|hair_care|hardware_store|health|hindu_temple|home_goods_store|hospital|insurance_agency|jewelry_store|laundry|lawyer|library|liquor_store|local_government_office|locksmith|lodging|meal_delivery|meal_takeaway|mosque|movie_rental|movie_theater|moving_company|museum|night_club|painter|park|parking|pet_store|pharmacy|physiotherapist|place_of_worship|plumber|police|post_office|real_estate_agency|restaurant|roofing_contractor|rv_park|school|shoe_store|shopping_mall|spa|stadium|storage|store|subway_station|synagogue|taxi_stand|train_station|travel_agency|university|veterinary_care|zoo";
resourceURI = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location="+myLocation.latitude+","+myLocation.longitude+"&radius=500&rankBy=distance&types="+ URLEncoder.encode(types, "UTF-8")+"&sensor=true&key=GOOGLE_MAPS_KEY";
try {
            String url =resourceURI; //getURL(myLocation.latitude,myLocation.longitude);

            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
            HttpConnectionParams.setSoTimeout(httpParams, 30000);
            DefaultHttpClient httpClient = new DefaultHttpClient(httpParams);
            HttpGet httpGet = new HttpGet(url);
            httpGet.setHeader("Content-type", "application/json");
            ResponseHandler responseHandler = new BasicResponseHandler();
            String response = (String) httpClient.execute(httpGet, responseHandler);

            if (response != null) {
                mResult = new JSONObject(response);
                results = mResult.getJSONArray("results");
            }
        }
        catch (ClientProtocolException e) {
            e.printStackTrace();
            return null;
        }
        catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        catch (JSONException e) {
            e.printStackTrace();
            return null;
        }

        return results;
    }
这将返回有效的JSON,但不是距离传入的距离最近的位置。我知道有一个事实,那就是有比请求返回的位置更近的地方


例如,我在一个已知的谷歌地点提出请求,但它并没有显示我目前所在的地点,而是显示更远的其他地点

很有可能你认为比返回的位置更近的地方不是真正的谷歌位置

查看此常见问题解答,在谷歌地图上获取您的业务:


这方面的一个测试可能是尝试注册你认为与GooglePlaces更接近的业务,看看它是否允许你注册。如果可以,这将意味着它们不是真正的Google place,也不会出现在这个结果集中。

也许您已经解决了您的问题,但我希望这可以帮助(关注粗体部分)

半径-定义返回放置结果的距离(以米为单位)。最大允许半径为50000米请注意,如果指定了rankby=距离(如下可选参数下所述),则不得包括半径。

rankby-指定结果的列出顺序。可能的值为:

突出显示(默认设置)。此选项根据结果的重要性对结果进行排序。排名将有利于指定区域内的突出位置。显著性可能受到位置在谷歌索引中的排名、应用程序的登录次数、全球知名度以及其他因素的影响

距离。此选项根据结果到指定位置的距离按升序对结果进行排序。指定距离时,需要一个或多个关键字、名称或类型。

根据:

我已经了解到,根据谷歌文档,你不能同时发送参数“rankby”和“radius”,你必须同时使用其中一个,这样你将得到按距离排序的结果

执行以下操作测试请求:

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

看看你怎么样,祝你好运

希望此代码能正常工作。

    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;
    }}
private ArrayList排序位置(LatLng currentLatLng,ArrayList alLocationDTO)
{
ArrayList clPlaceDTOArrayList;
ArrayList clPlaceDTOArrayList1=新的ArrayList();
double dCurrentLat=currentLatLng.纬度;
double dCurrentLong=currentLatling.longitude;
迭代器clIterator=alLocationDTO.Iterator();
while(clitter.hasNext())
{
clIterator.next();
clPlaceDTOArrayList=新的ArrayList();
对于(int j=0;j
Hey@Karios,事实上,我确实验证了我所期待的地点是在Google places上。你还有其他想法吗?我看到了