如何创建Android谷歌地图自定义地方选择器

时间:2017-02-09 14:21:06

标签: android android-custom-view

Android google map custom place picker like this

我正在创建一个应用程序,从Google地图中选择地点并将地址存储在数据库中。我想创建自定义地方选择器。我是Android开发的新手,请帮我创建自定义 place picker like this 。 xml布局

请帮我处理任何示例代码。这是我的java代码:

public class MainActivity extends AppCompatActivity implements PlaceSelectionListener {

private static final String LOG_TAG = "PlaceSelectionListener";
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
        new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
private static final int REQUEST_SELECT_PLACE = 1000;
private TextView locationTextView;
private TextView attributionsTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    locationTextView = (TextView) findViewById(R.id.txt_location);
    attributionsTextView = (TextView) findViewById(R.id.txt_attributions);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);

    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Method #2
            try {
                Intent intent = new PlaceAutocomplete.IntentBuilder
                        (PlaceAutocomplete.MODE_FULLSCREEN)
                        .setBoundsBias(BOUNDS_MOUNTAIN_VIEW)
                        .build(MainActivity.this);
                startActivityForResult(intent, REQUEST_SELECT_PLACE);
            } catch (GooglePlayServicesRepairableException |
                    GooglePlayServicesNotAvailableException e) {
                e.printStackTrace();
            }
        }
    });
}

@Override
public void onPlaceSelected(Place place) {
    Log.i(LOG_TAG, "Place Selected: " + place.getName());
    locationTextView.setText(getString(R.string.formatted_place_data, place
            .getName(), place.getAddress(), place.getPhoneNumber(), place
            .getWebsiteUri(), place.getRating(), place.getId()));
    if (!TextUtils.isEmpty(place.getAttributions())){
        attributionsTextView.setText(Html.fromHtml(place.getAttributions().toString()));
    }
}

@Override
public void onError(Status status) {
    Log.e(LOG_TAG, "onError: Status = " + status.toString());
    Toast.makeText(this, "Place selection failed: " + status.getStatusMessage(),
            Toast.LENGTH_SHORT).show();
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_SELECT_PLACE) {
        if (resultCode == RESULT_OK) {
            Place place = PlaceAutocomplete.getPlace(this, data);
            this.onPlaceSelected(place);
        } else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
            Status status = PlaceAutocomplete.getStatus(this, data);
            this.onError(status);
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

0 个答案:

没有答案