放置選擇器用法示例
Place Picker 是 Places API 提供的一個非常簡單的 UI 小部件。它提供內建地圖,當前位置,附近地點,搜尋能力和自動完成功能。
這是 Place Picker UI 小部件的示例用法。
private static int PLACE_PICKER_REQUEST = 1;
private TextView txtPlaceName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_place_picker_sample);
txtPlaceName = (TextView) this.findViewById(R.id.txtPlaceName);
Button btnSelectPlace = (Button) this.findViewById(R.id.btnSelectPlace);
btnSelectPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openPlacePickerView();
}
});
}
private void openPlacePickerView(){
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(this, data);
Log.i(LOG_TAG, String.format("Place Name : %s", place.getName()));
Log.i(LOG_TAG, String.format("Place Address : %s", place.getAddress()));
Log.i(LOG_TAG, String.format("Place Id : %s", place.getId()));
txtPlaceName.setText(String.format("Place : %s - %s" , place.getName() , place.getAddress()));
}
}
}