Place Autocomplete是Google Maps Platform提供的一项服务,它可以根据用户输入的部分地址信息实时预测并返回完整的地址建议。结合Directions API,可以实现从自动完成的起点和终点获取路线导航的功能。
首先需要在build.gradle
中添加依赖:
implementation 'com.google.android.libraries.places:places:3.1.0'
implementation 'com.google.maps.android:android-maps-utils:2.4.0'
implementation 'com.google.android.gms:play-services-maps:18.1.0'
implementation 'com.google.android.gms:play-services-location:21.0.1'
在Application类或第一个Activity中初始化:
// 初始化Places SDK
Places.initialize(getApplicationContext(), "YOUR_API_KEY");
// 设置自动完成意图
List<Place.Field> fields = Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG);
// 启动自动完成意图
Intent intent = new Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN,
fields)
.build(this);
startActivityForResult(intent, AUTOCOMPLETE_REQUEST_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AUTOCOMPLETE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Place place = Autocomplete.getPlaceFromIntent(data);
// 处理选中的地点
LatLng latLng = place.getLatLng();
String placeName = place.getName();
// 保存为起点或终点
if (isStartPoint) {
startLatLng = latLng;
startPlaceName = placeName;
} else {
endLatLng = latLng;
endPlaceName = placeName;
}
// 如果起点和终点都有了,调用Directions API
if (startLatLng != null && endLatLng != null) {
getDirections(startLatLng, endLatLng);
}
}
}
}
private void getDirections(LatLng origin, LatLng destination) {
// 创建Directions API请求
String originStr = origin.latitude + "," + origin.longitude;
String destinationStr = destination.latitude + "," + destination.longitude;
// 使用Volley或其他HTTP客户端发起请求
String url = "https://maps.googleapis.com/maps/api/directions/json?" +
"origin=" + originStr +
"&destination=" + destinationStr +
"&key=YOUR_API_KEY";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// 解析路线数据
parseDirectionResponse(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
// 添加到请求队列
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);
}
private void parseDirectionResponse(JSONObject response) throws JSONException {
JSONArray routes = response.getJSONArray("routes");
JSONObject route = routes.getJSONObject(0);
JSONObject overviewPolyline = route.getJSONObject("overview_polyline");
String points = overviewPolyline.getString("points");
// 解码polyline点
List<LatLng> path = PolyUtil.decode(points);
// 在地图上绘制路线
PolylineOptions options = new PolylineOptions()
.addAll(path)
.width(10)
.color(Color.BLUE)
.geodesic(true);
mMap.addPolyline(options);
// 移动相机以显示整个路线
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (LatLng point : path) {
builder.include(point);
}
LatLngBounds bounds = builder.build();
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
}
问题:收到"API key not valid"错误 原因:API密钥未正确配置或未启用相关API 解决:
问题:Place Autocomplete不返回任何结果 原因:可能由于区域限制或输入内容不符合要求 解决:
问题:路线显示不正确或中断 原因:Polyline解码错误或坐标点顺序问题 解决:
通过以上实现,你可以在Android应用中创建一个完整的地址自动完成和路线导航功能,为用户提供流畅的地图体验。
没有搜到相关的文章