当前位置: 代码迷 >> 综合 >> 谷歌google place map 地址选择器
  详细解决方案

谷歌google place map 地址选择器

热度:27   发布时间:2023-12-17 05:08:34.0
//谷歌地址选择器, PlaceAutocomplete 已经过时被废弃?用不了了。用最新的AutocompleteMigration Guide:-https://developers.google.com/places/android-sdk/client-migration Follows this steps to implement AutoComplete in your project :-There is two way to implement to AutoComplete .1. Using Intent2. Using AutocompleteFragmentIn  both case follow these steps:-1. Add this line in build.gradle file//主要是这个dependencies {implementation 'com.google.android.libraries.places:places:1.0.0'}2. Add other dependencies if required in your project.//可以不需要dependencies {implementation 'com.google.android.libraries.places:places-compat:1.0.0'}//可以不需要implementation 'com.google.android.gms:play-services-places:16.0.0'3. // Add an import statement for the client library.import com.google.android.libraries.places.api.Places;// Initialize Places.Places.initialize(getApplicationContext(), apiKey);// Create a new Places client instance.PlacesClient placesClient = Places.createClient(this);----------Now Choose any of the method you want to implement in your project.-----## Using Intent ##//必须要初始化Places,否则运行出错。在application执行。先执行下面的语句,再执行createClient()if (!Places.isInitialized()) {Places.initialize(getApplicationContext(), "YOUR_API_KEY");}...// Set the fields to specify which types of place data to return.List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME);// Start the autocomplete intent.Intent intent = new Autocomplete.IntentBuilder(AutocompleteActivityMode.FULLSCREEN, fields).build(this);startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);and OnActivityResult method-------/*** Override the activity's onActivityResult(), check the request code, and* do something with the returned place data (in this example it's place name and place ID).*/@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {if (resultCode == RESULT_OK) {Place place = Autocomplete.getPlaceFromIntent(data);Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());} else if (resultCode == AutocompleteActivity.RESULT_ERROR) {// TODO: Handle the error.Status status = Autocomplete.getStatusFromIntent(data);Log.i(TAG, status.getStatusMessage());} else if (resultCode == RESULT_CANCELED) {// The user canceled the operation.}}}## Using Fregment ##Initialize this Autocomplete Fregment on .xml file<fragmentandroid:id="@+id/autocomplete_fragment"android:layout_width="match_parent"android:layout_height="wrap_content"android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"/>-------------- and on .class file-------------------------- Initialize Places, passing the application context and your API key.
Initialize the AutocompleteSupportFragment.Call setPlaceFields() to indicate the types of place data that you want to get.
Add a PlaceSelectionListener to do something with the result, as well as handle any errors that might occur.The following example shows adding an autocomplete widget to an activity:/*** Initialize Places. For simplicity, the API key is hard-coded. In a production* environment we recommend using a secure mechanism to manage API keys.*/if (!Places.isInitialized()) {Places.initialize(getApplicationContext(), "YOUR_API_KEY");
}// Initialize the AutocompleteSupportFragment.AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {@Overridepublic void onPlaceSelected(Place place) {// TODO: Get info about the selected place.Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());}@Overridepublic void onError(Status status) {// TODO: Handle the error.Log.i(TAG, "An error occurred: " + status);}
});
  相关解决方案