//谷歌地址選擇器, PlaceAutocomplete 已經過時被廢棄?用不了了。用最新的Autocomplete
Migration 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 Intent
2. Using AutocompleteFragment
In 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).
*/
@Override
protected 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
<fragment
android: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() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName() + ", " + place.getId());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
谷歌google place map 地址選擇器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.