マサルの備忘録

ハイオク仕様のトルクフルな備忘録

Places APIをなんとなく使ってみた

スポンサーリンク

便利なような。便利じゃないような。

Places APIとは?

developers.google.com

いわゆるWebAPI的に、特定のアドレスにPOSTしてJSONやらXMLでやりとりする真面目な方ではなくて、Google Play Servicesに含まれる便利クラスを使ったゆとり向けAPIです。

3月にリリースされたGoogle Play Services 7.0から使えるようになった新機能です。

何ができるの?

www.youtube.com

これ見ておいてください。

www.youtube.com

Google Play Services 7.0の説明動画は、色んな意味で必見です。
Magnus Hyttsten氏、4.4の説明動画は真面目な感じだったんだけどな。

実際にやってみた

f:id:domomasarudesu:20150324233107p:plain

アクティビティでPlacePickerクラスを使ってIntentを作成。
startActivity()で、作成したIntentで別アクティビティに飛びます。

f:id:domomasarudesu:20150324233236p:plain

遷移先では、現在地周辺の建物と地図が表示されます。

f:id:domomasarudesu:20150324233314p:plain

テキトーに建物を選ぶと・・・

f:id:domomasarudesu:20150324233334p:plain

元のアクティビティに戻ってきます。
その際に、onActivityResult()で選択した建物のPlaceオブジェクトが得られるので、建物名だったり住所だったりが取得できます。

いいところ/わるいところ

すっごく簡単に建物を指定できます。しかもUIを作ることなく。
ただ、やっぱりいいことばかりではありません。

  • 便利クラスなので、あまりカスタマイズできません(例えば遷移先のUIなど)
  • 地名・建物名・住所などの言語が変えられません(わざと英語名が欲しい場合などは困る)
  • Google Play Servicesなので、いつDeprecatedになるかよくわからない(通常APIもそうですが)

Program上で地名を取得する時は、意外とGeoCoderのほうが便利なのかなと思いつつ、建物名まで取得できるのはPlaces APIならではです。

アトリビューション?

Display attributions in your app

When your app displays information obtained via the place picker, the app must also display attributions. See the documentation on attributions.

ふーん。
ちょっとめんどくさいですね。(やべえ、つけてねえや・・・)

クソコード

ほぼサンプルコード(真顔)

package com.masaru.placesapitest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
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;

public class MainActivity extends Activity {

	private final int REQUEST = 1;

	private Button mButton1;

	private TextView mTextView1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mButton1 = (Button) findViewById(R.id.button1);
		mTextView1 = (TextView) findViewById(R.id.textView1);

		mButton1.setOnClickListener(mClickListener);
	}

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

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

			Place place = PlacePicker.getPlace(data, this);
			mTextView1.setText(new StringBuilder().append(place.getName())
					.append("\n").append(place.getAddress()).toString());
		} else {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}

	private OnClickListener mClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			try {
				PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
				Intent intent = intentBuilder.build(MainActivity.this);
				startActivityForResult(intent, REQUEST);
			} catch (GooglePlayServicesRepairableException e) {
			} catch (GooglePlayServicesNotAvailableException e) {
			}
		}
	};
}