简体   繁体   中英

Google Places Search with Spinner

I'm currently using Google Places API in my app to run a query for nearby restaurants in a 5km radius. However, I would like to give the user the functionality to search in a 10km and 25km radius as well.

So I decided to use a spinner for that purpose. Enable the user to select the desired radius from the spinner and then update the result on the map. However, I've been unable to get it to work, I've tried a few things but have failed.

layout file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Restaurants in a"
            android:background="#0000"
            android:gravity="center"
            android:textSize="30dp" />

        <Spinner
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/locateSpinner"></Spinner>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="radius"
            android:textSize="30dp"
            android:background="#0000"/>

    </LinearLayout>


    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        tools:context="com.test.drawernav.LocateFragment"/>


</LinearLayout>

Relevant portions of Java file

@Override
    public View onCreate(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Spinner spinner = (Spinner)getActivity().findViewById(R.id.locateSpinner);
        spinner.setOnItemSelectedListener(this);

        List<String> distances = new ArrayList<String>();
        distances.add("5km");
        distances.add("10km");
        distances.add("25km");

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, distances);

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner.setAdapter(dataAdapter);

                mapFrag = (SupportMapFragment) this.getChildFragmentManager()
                        .findFragmentById(R.id.map);
        mapFrag.getMapAsync(this);
    }

public StringBuilder sbMethod(Location currentLocation) {
        //current location
        double mLatitude = currentLocation.getLatitude();
        double mLongitude = currentLocation.getLongitude();


        StringBuilder sb = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
        sb.append("location=" + mLatitude + "," + mLongitude);
        sb.append("&radius=5000");
        sb.append("&types=restaurant");
        sb.append("&sensor=true");
        sb.append("&key=***");

        Log.d("Map", "<><>api: " + sb.toString());


        return sb;
        }

@Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {


    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

I know that I need to get the Stringbuilder to run separately with the radius parameter changed to '5000', '10000' and '25000' in all the three instances when '5km', '10km' and '25km' is selected from the Spinner. I'm just not sure how to go about doing it.

Create a global variable for radius and setOnItemSelectedListener() method put the selected value of radius in that variable and pass that value in this line

sb.append("&radius=5000"); as sb.append("&radius="+selected_radius);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM