LoginSignup
15
17

More than 5 years have passed since last update.

PlaceAutocompleteFragmentを試してみた

Posted at

PlaceAutocompleteFragmentとは

Google places API for Androidのサービスの一つで、ユーザが場所の名前や住所を入力している途中で、入力に基づいたプレイスの予測候補を返してくれます。これを利用すればアプリ内で場所や住所の入力にかかる時間を短縮することができます。
PlaceAutocompleteFragmentを呼び出すだけで簡単に上記の機能を実現できます。
よくSNSなどで見かけるチェックイン機能のようなものです。

alt

Google Places APIとは

API内で施設、地理的位置、有名なスポットとして定義されている場所の情報を取得できるサービスです。ユーザの現在地または検索文字列に基づいてプレイスの一覧を取得したり、ユーザのクチコミなど特定のプレイスに関する詳細情報を取得できます。

Google Play ServicesとCardviewの導入

build.gradle
dependencies {
   compile 'com.google.android.gms:play-services:9.4.0'
   compile 'com.android.support:cardview-v7:24.1.1'
}

※Cardviewは見やすくするために導入しました。

実装

MainActivity.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;

public class MainActivity extends AppCompatActivity implements PlaceSelectionListener {

private TextView mPlaceInfoText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPlaceInfoText = (TextView)findViewById(R.id.placeInfoTextView);
        // Retrieve the PlaceAutocompleteFragment.
        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
        autocompleteFragment.setOnPlaceSelectedListener(this);
    }

    @Override
    public void onPlaceSelected(Place place) {
        // Format the returned place's details and display them in the TextView.
        String info = "name = " + place.getName() + "\n";
        info += "id = " + place.getId() + "\n";
        info += "address = " + place.getAddress() + "\n";
        info += "phone number = " + place.getPhoneNumber() + "\n";
        info += "web site url = " + place.getWebsiteUri() + "\n";
        mPlaceInfoText.setText(info);
    }

    @Override
    public void onError(Status status) {

    }
}

Placeクラスでは他にも場所の緯度経度や評価、種類(お店、駐車場、公園)などを取得することができます。

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="jp.flatfish.placesapi.MainActivity">

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

        <fragment
            android:id="@+id/autocomplete_fragment"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"/>
    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/placeInfoTextView"
        android:layout_centerInParent="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>   
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jp.flatfish.placesapi">

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

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

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

参照元

GitHub - googlesamples/android-play-places
プレイス オートコンプリート| Google Places API for Android | GoogleDevelopers
Google Places API  |  Google Developers

協力

この記事はGDG石巻の協力の元作成されました。
GDG石巻とは?

15
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
17