我是学习Google Places API的新手,并且一直在寻找这个问题的答案而没有太多运气。
因此,在我的应用程序的第一部分中,我希望检索当前位置的placeTypes。我已成功完成此操作,但它返回一个Integer值列表,然后与不同的Place.TYPE_ * s相关联。
我的总体目标是获取我所在位置的当前placeTypes,然后通过网址进行PlaceSearch。为此,我需要将位置列表中的整数转换为字符串类型,以便将其插入到我的URL中。
注意:我尝试插入" restaurant"的整数值。而不只是"餐厅"在URL中,但它返回了错误的结果,例如酒店,办公楼,当我使用该词时未返回。
例如,我的网址如下:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + lat + "," + lng+"&radius=2000&type=restaurant&key=my_key
注意"餐厅"在URL中。这就是我用Integer值替换它的地方,它没有返回正确的结果。
答案 0 :(得分:1)
<强> [EDITED] 强>
你可以试试这个。
private String getPlaceTypeForValue(int value) throws Exception {
Field[] fields = Place.class.getDeclaredFields();
String name;
for (Field field : fields) {
name = field.getName().toLowerCase();
if (name.startsWith("type_") && field.getInt(null) == value) {
return name.replace("type_", "");
}
}
throw new IllegalArgumentException("place value " + value + " not found.");
}
我创建了一个测试来验证它,应该有用,只是遗漏了一些类型:administrative_area_level_4
,administrative_area_level_5
,postal_code_suffix
和street_number
。
public class ConvertingPlaceTypesUnitTest {
private enum 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,
administrative_area_level_3,
// administrative_area_level_4, Not in Place.class
// administrative_area_level_5, Not in Place.class
colloquial_area,
floor,
geocode,
intersection,
natural_feature,
neighborhood,
political,
point_of_interest,
post_box,
postal_code_prefix,
// postal_code_suffix, Not in Place.class
postal_town,
premise,
room,
route,
street_address,
// street_number, Not in Place.class
sublocality_level_4,
sublocality_level_5,
sublocality_level_3,
sublocality_level_2,
sublocality_level_1,
subpremise,
transit_station,
locality,
sublocality,
postal_code,
country,
administrative_area_level_1,
administrative_area_level_2
};
@Test
public void extracPlaces() throws Exception {
assertEquals("accounting", getPlaceTypeForValue(Place.TYPE_ACCOUNTING));
assertEquals("local_government_office", getPlaceTypeForValue(Place.TYPE_LOCAL_GOVERNMENT_OFFICE));
}
@Test
public void testAllTypes() throws Exception {
for (Types type : Types.values()) {
final String name = type.toString();
int value = getPlaceTypeValue("TYPE_" + name.toUpperCase());
assertEquals(name, getPlaceTypeForValue(value));
}
}
private int getPlaceTypeValue(String fieldName) throws Exception {
Field field = Place.class.getDeclaredField(fieldName);
return field.getInt(null);
}
private String getPlaceTypeForValue(int value) throws IllegalAccessException {
Field[] fields = Place.class.getDeclaredFields();
String name;
for (Field field : fields) {
name = field.getName().toLowerCase();
if (name.startsWith("type_") && field.getInt(null) == value) {
return name.replace("type_", "");
}
}
throw new IllegalArgumentException("place value " + value + " not found.");
}
}
<强> [老] 强>
我不明白类型整数的来源,在文档中它总是被称为字符串。
type - 将结果限制为与指定类型匹配的位置。只能指定一种类型(如果提供了多种类型,则忽略第一个条目后面的所有类型)。请参阅list of supported types。
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "48",
"short_name" : "48",
"types" : [ "street_number" ]
},
{
"long_name" : "Pirrama Road",
"short_name" : "Pirrama Road",
"types" : [ "route" ]
},
{
"long_name" : "Pyrmont",
"short_name" : "Pyrmont",
"types" : [ "locality", "political" ]
},
{
"long_name" : "NSW",
"short_name" : "NSW",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "AU",
"short_name" : "AU",
"types" : [ "country", "political" ]
},
{
"long_name" : "2009",
"short_name" : "2009",
"types" : [ "postal_code" ]
}
],
"adr_address" : "5,
\u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e,
\u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e
\u003cspan class=\"region\"\u003eNSW\u003c/span\u003e
\u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e,
\u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address" : "48 Pirrama Road, Pyrmont NSW, Australia",
"formatted_phone_number" : "(02) 9374 4000",
"geometry" : {
"location" : {
"lat" : -33.8669710,
"lng" : 151.1958750
},
"viewport" : {
"northeast" : {
"lat" : -33.8665053,
"lng" : 151.1960371
},
"southwest" : {
"lat" : -33.8669293,
"lng" : 151.1952183
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number" : "+61 2 9374 4000",
"name" : "Google Sydney",
"place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
"scope" : "GOOGLE",
"alt_ids" : [
{
"place_id" : "D9iJyWEHuEmuEmsRm9hTkapTCrk",
"scope" : "APP"
}
],
"rating" : 4.70,
"reference" : "CnRsAAAA98C4wD-VFvzGq-KHVEFhlHuy1TD1W6UYZw7KjuvfVsKMRZkbCVBVDxXFOOCM108n9PuJMJxeAxix3WB6B16c1p2bY1ZQyOrcu1d9247xQhUmPgYjN37JMo5QBsWipTsnoIZA9yAzA-0pnxFM6yAcDhIQbU0z05f3xD3m9NQnhEDjvBoUw-BdcocVpXzKFcnMXUpf-nkyF1w",
"reviews" : [
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Simon Bengtsson",
"author_url" : "https://plus.google.com/104675092887960962573",
"language" : "en",
"rating" : 5,
"text" : "Just went inside to have a look at Google. Amazing.",
"time" : 1338440552869
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Felix Rauch Valenti",
"author_url" : "https://plus.google.com/103291556674373289857",
"language" : "en",
"rating" : 5,
"text" : "Best place to work :-)",
"time" : 1338411244325
},
{
"aspects" : [
{
"rating" : 3,
"type" : "quality"
}
],
"author_name" : "Chris",
"language" : "en",
"rating" : 5,
"text" : "Great place to work, always lots of free food!",
"time" : 1330467089039
}
],
"types" : [ "establishment" ],
"url" : "http://maps.google.com/maps/place?cid=10281119596374313554",
"vicinity" : "48 Pirrama Road, Pyrmont",
"website" : "http://www.google.com.au/"
},
"status" : "OK"
}