android - 谷歌地图:绘制路线起点和终点相连
问题描述
我正在使用航点在谷歌地图上绘制路线。一切看起来都不错,除了我的出发地和目的地也相连。我确信我的出发地和目的地不一样。
这是我从纬度/经度列表生成路线 url 后生成的 url。
https://maps.googleapis.com/maps/api/directions/json?&origin=26.8530478,75.78491989999999 &destination=26.8804683,75.75850109999999 &waypoints=26.8566917,75.7691974%7C 26.868405,75.76440269999999%7C 26.8762989,75.7664082 &key=MY_API_KEY
并附上输出。从“1”到“5”的直线不应该在那里。请帮我找出错误。
谢谢。
解析器任务 -
ArrayList<LatLng> points = new ArrayList<LatLng>();
PolylineOptions lineOptions = new PolylineOptions();
// LatLngBounds.Builder builder = new LatLngBounds.Builder();
int color = ContextCompat.getColor(activity, R.color.black_overlay);
if (null != result)
for (int i = 0; i < result.size(); i++) {
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
//builder.include(position);
}
lineOptions = new PolylineOptions().width(15).color(color).addAll(points).zIndex(6);
}
if (activity instanceof OnRoutesParsingCompletedCallBack) {
((OnRoutesParsingCompletedCallBack) activity).
onRoutesParsingCompleted(lineOptions);
}
带有地图对象的活动 -
@Override
public void onRoutesParsingCompleted(PolylineOptions lineOptions) {//}, LatLngBounds.Builder builder) {
if (lineOptions.getPoints().size() > 0) {
googleMap.addPolyline(lineOptions);
} else
Toast.makeText(this, "There is something wrong. No routes to draw!", Toast.LENGTH_SHORT).show();
mCurrLocationMarker.showInfoWindow();
progressBar.setVisibility(GONE);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLong, 13));
}
解决方案
我认为,问题在于您将所有可能的路线添加到同一条折线。因此,折线经过第一条路线,然后“返回”到起点并再次经过第二条路线。您要么需要删除结果中的 for 循环:
if (null != result)
// Fetching 1st route
List<HashMap<String, String>> path = result.get(0);
// Fetching all the points in 1st route
for (int j = 0; j < path.size(); j++) {
HashMap<String, String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
//builder.include(position);
}
lineOptions = new PolylineOptions().width(15).color(color).addAll(points).zIndex(6);
}
或者,您可以为每条现有路线创建一条折线。
推荐阅读
- javascript - Firebase如何更新javascript中的子值
- java - 如何从android中的数组数组中获取Json数据
- javascript - 使用嵌套键减少 Javascript 对象数组
- hive - 麒麟基数计算
- javascript - 将外部 JS 导入 Angular 8
- c - 迭代方法到递归方法 C 语言
- python - 如何使用来自多个目录的 cogs - Discord.py
- java - 创建公告板时,我在问如何设置 id 和 post number 主键
- swiftui - SwiftUI 的 List/ForEach 视图中是否可以有一个 Button(action: -)
- python - Streamlit 中的登录页面问题