Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – Google Places Api按距离排序大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
目前正在开发一个 Android应用程序,它将最近的20位置返回给用户当前位置.

Google Places API返回约20个位置靠近用户位置,但不是最近的20个按距离排序.

Google Places API Documentation并没有显示任何我认为不正确的内容.

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,但不是返回传入距离的最近位置.我知道有一个事实,那里的地方比请求返回的地方更近.

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

解决方法

也许你已经解决你的问题,但我希望这可以帮助(专注于粗体):

radius – 定义返回Place结果的距离(以米为单位).允许的最大宽度为50 000米.注意如果指定了rankby = distance(在可选参数下面描述),则不得包括半径.

rankby – 指定列出结果的顺序.可能的值是:

突出(认).此选项根据结果的重要性对结果进行排序.排名将有利于指定区域内的显着位置.地方在Google索引中的排名,应用程序的签到数量,全球受欢迎程度以及其他因素都会影响显着性.

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

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

我已经根据谷歌文档了解到你不能同时发送参数“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";

祝你好运,祝你好运!

大佬总结

以上是大佬教程为你收集整理的android – Google Places Api按距离排序全部内容,希望文章能够帮你解决android – Google Places Api按距离排序所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。