谷歌放置API返回单一类型“car_repair”

2022-09-04 04:07:26

我正在使用Google places API,它正在返回我所在位置的所有位置。然而,我只想让它返回一种“汽车修理”的类型,我想我几乎拥有它,但我错过了一些东西,如果有人能引导我朝着正确的方向前进,那将是很棒的:)

到目前为止,我的代码 PlacPickerActivity

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 PlacePickerActivity extends AppCompatActivity {

    private static final int PLACE_PICKER_REQUEST = 1;
    String url="https://maps.googleapis.com/maps/api/place/textsearch/json?type=car_repair&key=AIzaSyBKsTtLyMBQH8mhvbknJ4MvZwACotmeYO0";

    private TextView mName;
    private TextView mAddress;
    private TextView mAttributions;
    private TextView mNumber;
    private static final LatLngBounds Sligo = new LatLngBounds(
            new LatLng(54.27, -8.47), new LatLng(54.27, -8.47));

    @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);
        mNumber = (TextView) findViewById(R.id.textView4);
        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(Sligo);

                    Intent intent = intentBuilder.build(PlacePickerActivity.this);
                    startActivityForResult(intent, PLACE_PICKER_REQUEST);

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

    @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();
            final CharSequence formatted_phone_number = place.getPhoneNumber();
            String attributions = (String) place.getAttributions();
            if (attributions == null) {
                attributions = "";
            }

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


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

}

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.truiton.placepicker">

    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyBKsTtLyMBQH8mhvbknJ4MvZwACotmeYO0"/>

        <activity
            android:name=".PlacePickerActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

当我在PC上的浏览器中键入URL时,它会在我的区域中返回所有类型“car_repair”,因此我的API密钥确实有效enter image description here


答案 1

尝试使用类似于以下内容的查询:位置采用纬度,经度格式...并注意是“类型”而不是类型。

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=54.27,-8.47&radius=1000&types=car_repair&key=AddYourOwnKeyHere

答案 2

因为此 API 返回作用域内的位置类型。有一种方法可以改善结果,但仍然不是最佳的,称为。该链接 https://developers.google.com/android/reference/com/google/android/gms/location/places/ui/PlacePicker.IntentBuilder#public-method-summary 我以前尝试过此API,发现它在某种程度上完全是一团糟。例如,如果您想在大学内找到餐厅,此API不仅会返回餐厅,还会返回许多其他类型,例如。设置一个边界可能很好,但如果你只使用API,仍然会得到令人讨厌的结果。如果你只是想要的类型,我建议你写一个过滤器来过滤其他类型,只保留汽车维修类型和相关信息。setLatLngBoundsUniversitycar-repair


推荐