java - 如何从 Google Places API 中删除不需要的地点?

标签 java android google-places-api

我为我的应用程序创建了一个 Google Places API,它目前显示附近的所有地点,如医院、餐馆、学校等。我希望它只显示餐馆。是否可以?如果是这样,怎么办?非常感谢您的帮助。谢谢:)

MapActivity.JAVA

package com.golo.acer.mrestro4;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;


public class MapActivity extends AppCompatActivity {
    private static final int PLACE_PICKER_REQUEST = 1;
    private TextView mName;
    private TextView mAddress;
    private TextView mAttributions;
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
            new LatLng(27.689753, 85.311188), new LatLng(27.689753, 85.311188));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_place_picker);
        mName = (TextView) findViewById(R.id.textView);
        mAddress = (TextView) findViewById(R.id.textView2);
        mAttributions = (TextView) findViewById(R.id.textView3);
        Button pickerButton = (Button) findViewById(R.id.pickerButton);
        pickerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    PlacePicker.IntentBuilder intentBuilder =
                            new PlacePicker.IntentBuilder();
                    intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
                    Intent intent = intentBuilder.build(MapActivity.this);
                    startActivityForResult(intent, PLACE_PICKER_REQUEST);

                } catch (GooglePlayServicesRepairableException
                        | GooglePlayServicesNotAvailableException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void moveToInternalChoiceActivity(View view) {
        Intent intent = new Intent(this, InternalChoiceActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode,
                                    int resultCode, Intent data) {

        if (requestCode == PLACE_PICKER_REQUEST
                && resultCode == Activity.RESULT_OK) {

            final Place place = PlacePicker.getPlace(this, data);
            final CharSequence name = place.getName();
            final CharSequence address = place.getAddress();
            String attributions = (String) place.getAttributions();
            if (attributions == null) {
                attributions = "";
            }

            mName.setText(name);
            mAddress.setText(address);
            mAttributions.setText(Html.fromHtml(attributions));

        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

}

最佳答案

place.getPlaceTypes() 返回一个整数列表,如果您想要查找餐厅类型,请检查它是否包含 Place.TYPE_RESTAURANT。您可以在 Place 类中获取以 TYPE_ 开头的类型值。

引用号https://developers.google.com/android/reference/com/google/android/gms/location/places/Place

[编辑] 在您的代码中:

final Place place = PlacePicker.getPlace(this, data);
List<Integer> types = place.getPlaceTypes();
for(Integer i : types){
    if (i == Place.TYPE_RESTAURANT){
           // its restaurant do something
        final CharSequence name = place.getName();
        final CharSequence address = place.getAddress();
        String attributions = (String) place.getAttributions();
        if (attributions == null) {
            attributions = "";
        }

        mName.setText(name);
        mAddress.setText(address);
        mAttributions.setText(Html.fromHtml(attributions));
    } 
}

[编辑:(来自用户的评论)]

您所描述的红色图标是一个标记,您可以将它们添加到 map 上。 因此,在您的 onActivityResult 中,您会得到一个 Place (已在您的代码中显示)。 地点包含名称和位置,使用它在 map 上显示标记,如下所示:

Marker markerPlace = map.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName));

查看 map 标记的文档和相关文章。

现在如何添加 map ?别问我,上网查资料 http://www.vogella.com/tutorials/AndroidGoogleMaps/article.html#mapsview

关于java - 如何从 Google Places API 中删除不需要的地点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39065545/

相关文章:

Java - 枚举作为可选

java - 如何让一个ImageView覆盖另一个View并在一定时间后隐藏?

android - android 中应用程序计费 v3 中的恢复功能的用途或含义是什么

java - 使用相机检测真实物体。使用相机寻找真实物体的可能性

javascript - 我可以按城镇限制谷歌地理编码器的结果吗?

javascript - 对于 google places API 中的英语本地化

java构建工具maven和jar文件

java - 从 Object 变量获取 TreeSet 对象

java - 使用 Criteria API 根据条件计算连接表的结果

url - Google Places API 按网址搜索