Android Open Source - AndroidPlaces Nearby Search Request






From Project

Back to project page AndroidPlaces.

License

The source code is released under:

Apache License

If you think the Android project AndroidPlaces listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2014 Brian Lee/*from   w  ww . j a  va  2  s  .c  o  m*/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tigerpenguin.places.request;

import android.content.Context;
import com.tigerpenguin.places.model.PlaceType;
import com.tigerpenguin.places.model.RankBy;
import com.tigerpenguin.places.response.NearbySearchResponse;
import com.tigerpenguin.places.response.NearbySearchResponseFactory;
import com.tigerpenguin.places.response.PlacesResponseFactory;
import com.tigerpenguin.places.responsehandler.PlacesResponseHandler;

public class NearbySearchRequest extends PlaceSearchRequest<NearbySearchRequest, NearbySearchResponse> {

    private static final String REQUEST_PATH = "nearbysearch/json?";
    private static final String PARAM_RANK_BY = "rankby";

    private final RankBy rankBy;

    /**
     * Creates a NearbySearchRequest ranked by prominence
     *
     * @param context
     * @param placesResponseHandler
     * @param latitude
     * @param longitude
     * @param radius
     */
    public NearbySearchRequest(Context context, PlacesResponseHandler<NearbySearchResponse> placesResponseHandler,
                               double latitude, double longitude, int radius) {
        super(context, placesResponseHandler);

        checkLatLong(latitude, longitude);
        checkRadius(radius);

        setParameter(PARAM_LOCATION, String.valueOf(latitude) + "," + String.valueOf(longitude));
        setParameter(PARAM_RADIUS, String.valueOf(radius));
        rankBy = RankBy.PROMINENCE;
        // prominence search is the default, so there is no need to set the parameter
    }

    /**
     * Creates a NearbySearchRequest ranked by distance.
     * One of keyword, name, or types must be defined.
     *
     * @param context
     * @param placesResponseHandler
     * @param latitude
     * @param longitude
     * @param keyword
     * @param name
     * @param placeTypes
     */
    public NearbySearchRequest(Context context, PlacesResponseHandler<NearbySearchResponse> placesResponseHandler,
                               double latitude, double longitude, String keyword, String name,
                               PlaceType... placeTypes) {
        super(context, placesResponseHandler);

        checkLatLong(latitude, longitude);
        if (isEmpty(keyword) && isEmpty(name) && (placeTypes == null || placeTypes.length == 0)) {
            throw new IllegalArgumentException("At least one of keyword, name, or types must be defined");
        }

        setParameter(PARAM_LOCATION, String.valueOf(latitude) + "," + String.valueOf(longitude));
        rankBy = RankBy.DISTANCE;
        setParameter(PARAM_RANK_BY, rankBy.getValue());

        if (!isEmpty(keyword)) {
            setParameter(PARAM_KEYWORD, keyword);
        }

        if (!isEmpty(name)) {
            setParameter(PARAM_NAME, name);
        }

        if (placeTypes != null && placeTypes.length > 0) {
            setParameter(PARAM_TYPES, typesToParameter(placeTypes));
        }
    }

    /**
     * Creates a next page search
     *
     * @param context
     * @param nextPageToken
     * @param placesResponseHandler
     */
    public NearbySearchRequest(Context context, String nextPageToken,
                               PlacesResponseHandler<NearbySearchResponse> placesResponseHandler) {
        super(context, nextPageToken, placesResponseHandler);
        rankBy = null;
    }

    public NearbySearchRequest setName(String name) {
        setParameter(PARAM_NAME, name);
        return this;
    }

    public NearbySearchRequest setTypes(PlaceType... placeTypes) {
        if (placeTypes != null) {
            setParameter(PARAM_TYPES, typesToParameter(placeTypes));
        } else {
            removeParameter(PARAM_TYPES);
        }
        return this;
    }

    public NearbySearchRequest setKeyword(String keyword) {
        setParameter(PARAM_KEYWORD, keyword);
        return this;
    }

    @Override
    protected String getRequestPath() {
        return REQUEST_PATH;
    }

    @Override
    protected PlacesResponseFactory<NearbySearchRequest, NearbySearchResponse> getResponseFactory() {
        return new NearbySearchResponseFactory();
    }

    @Override
    protected NearbySearchRequest getThis() {
        return this;
    }

    @Override
    protected void validate() throws InvalidRequestException {
        super.validate();

        if (isNextPageSearch()) {
            return;
        }

        checkParameterDefined(PARAM_LOCATION, "latitude and longitude must be defined");

        switch (rankBy) {
            case PROMINENCE:
                checkParameterDefined(PARAM_RADIUS, "radius must be defined");
                break;

            case DISTANCE:
                checkParameterDefined(PARAM_RANK_BY, "rankby must be defined");
                String keyword = getParameter(PARAM_KEYWORD);
                String name = getParameter(PARAM_NAME);
                String types = getParameter(PARAM_TYPES);

                if (isEmpty(keyword) && isEmpty(name) && isEmpty(types)) {
                    throw new InvalidRequestException(
                            "At least one of keyword, name, or types must be defined for distance search");
                }
                break;

            default:
                throw new InvalidRequestException("Unhandled rankby value: " + rankBy.getValue());
        }
    }
}




Java Source Code List

com.tigerpenguin.demo.places.PlaceDetailActivity.java
com.tigerpenguin.demo.places.PlaceResultAdapter.java
com.tigerpenguin.demo.places.PlaceResultsFragment.java
com.tigerpenguin.demo.places.PlaceReviewAdapter.java
com.tigerpenguin.demo.places.PlaceReviewsFragment.java
com.tigerpenguin.demo.places.PlaceThumbnailAdapter.java
com.tigerpenguin.demo.places.PlaceThumbnailsFragment.java
com.tigerpenguin.demo.places.PlacesDemo.java
com.tigerpenguin.places.PlacesTest.java
com.tigerpenguin.places.deserializer.HtmlStringDeserializer.java
com.tigerpenguin.places.deserializer.PlaceTypeDeserializer.java
com.tigerpenguin.places.model.AddressComponent.java
com.tigerpenguin.places.model.AspectRating.java
com.tigerpenguin.places.model.AspectType.java
com.tigerpenguin.places.model.DayOfWeek.java
com.tigerpenguin.places.model.DayTime.java
com.tigerpenguin.places.model.Geometry.java
com.tigerpenguin.places.model.JsonModel.java
com.tigerpenguin.places.model.Language.java
com.tigerpenguin.places.model.OpeningHours.java
com.tigerpenguin.places.model.Period.java
com.tigerpenguin.places.model.Photo.java
com.tigerpenguin.places.model.PlaceDetail.java
com.tigerpenguin.places.model.PlaceLocation.java
com.tigerpenguin.places.model.PlaceType.java
com.tigerpenguin.places.model.Place.java
com.tigerpenguin.places.model.PriceLevel.java
com.tigerpenguin.places.model.RankBy.java
com.tigerpenguin.places.model.Review.java
com.tigerpenguin.places.model.StatusCode.java
com.tigerpenguin.places.request.InvalidRequestException.java
com.tigerpenguin.places.request.NearbySearchRequestTest.java
com.tigerpenguin.places.request.NearbySearchRequest.java
com.tigerpenguin.places.request.PlaceDetailRequestTest.java
com.tigerpenguin.places.request.PlaceDetailRequest.java
com.tigerpenguin.places.request.PlaceSearchRequestTest.java
com.tigerpenguin.places.request.PlaceSearchRequest.java
com.tigerpenguin.places.request.PlacesRequestTest.java
com.tigerpenguin.places.request.PlacesRequest.java
com.tigerpenguin.places.request.RequestTest.java
com.tigerpenguin.places.response.NearbySearchResponseFactory.java
com.tigerpenguin.places.response.NearbySearchResponse.java
com.tigerpenguin.places.response.PlaceDetailResponseFactory.java
com.tigerpenguin.places.response.PlaceDetailResponse.java
com.tigerpenguin.places.response.PlaceSearchResponse.java
com.tigerpenguin.places.response.PlacesResponseFactory.java
com.tigerpenguin.places.response.PlacesResponse.java
com.tigerpenguin.places.responsehandler.NearbySearchHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlaceDetailResponseHandlerFragment.java
com.tigerpenguin.places.responsehandler.PlacesResponseHandler.java
com.tigerpenguin.widget.simpleratingbar.MockAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarAttributes.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBarTest.java
com.tigerpenguin.widget.simpleratingbar.SimpleRatingBar.java