Back to project page AndroidPlaces.
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.
/* * 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()); } } }