在Android中使用Place Autocomplete实现Google Maps Directions API,可以通过以下步骤完成:
dependencies {
implementation 'com.google.android.gms:play-services-maps:17.0.0'
implementation 'com.google.android.gms:play-services-location:18.0.0'
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY" />
<service
android:name=".MyPlacesAutocompleteService"
android:exported="false" />
</application>
<EditText
android:id="@+id/editTextPlace"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search" />
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.AutocompleteFilter;
import com.google.android.gms.location.places.AutocompletePrediction;
import com.google.android.gms.location.places.AutocompletePredictionBuffer;
import com.google.android.gms.location.places.GeoDataClient;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
private EditText editTextPlace;
private Button buttonSearch;
private GeoDataClient geoDataClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextPlace = findViewById(R.id.editTextPlace);
buttonSearch = findViewById(R.id.buttonSearch);
geoDataClient = Places.getGeoDataClient(this);
buttonSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String query = editTextPlace.getText().toString();
AutocompleteFilter filter = new AutocompleteFilter.Builder()
.setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
.build();
Task<AutocompletePredictionBuffer> task = geoDataClient.getAutocompletePredictions(query, null, filter);
task.addOnCompleteListener(new OnCompleteListener<AutocompletePredictionBuffer>() {
@Override
public void onComplete(@NonNull Task<AutocompletePredictionBuffer> task) {
if (task.isSuccessful()) {
AutocompletePredictionBuffer predictions = task.getResult();
for (AutocompletePrediction prediction : predictions) {
// 处理地点建议
String placeId = prediction.getPlaceId();
String placeName = prediction.getPrimaryText(null).toString();
Log.d("Place", "Place ID: " + placeId);
Log.d("Place", "Place Name: " + placeName);
}
predictions.release();
} else {
Status status = task.getException().getStatus();
Log.e("Place", "Error getting autocomplete predictions: " + status.getStatusMessage());
}
}
});
}
});
}
}
以上代码演示了如何使用Place Autocomplete API获取地点建议,并在控制台中打印出地点的ID和名称。你可以根据自己的需求进一步处理地点建议,例如显示在自动完成列表中,或者选择一个地点后获取其详细信息。
推荐的腾讯云相关产品:腾讯位置服务(https://cloud.tencent.com/product/lbs)
腾讯云位置服务(Tencent Location Service)是一款提供位置信息的云服务,包括地理编码、逆地理编码、周边搜索、路径规划等功能。它可以帮助开发者在应用中实现类似Place Autocomplete的功能,并提供了丰富的API和SDK供开发者使用。
领取专属 10元无门槛券
手把手带您无忧上云