放置选择器用法示例
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()));
}
}
}