Google 地图:绘制路线起点和终点相连
Google Maps: Drawing routes origin and destination are connected
我正在使用 waypoints 在 google 地图上绘制路线。一切看起来都很好,除了我的出发地和目的地也相连。我确定我的出发地和目的地不一样。
这是我从 latitude/longitudes 的列表生成路线 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);
}
Activity 与地图对象 -
@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));
}
我认为,问题在于您将所有可能的路线添加到同一条折线。因此,多段线走第一条路线,然后 "returns" 到起点,然后再走第二条路线。您要么需要删除在结果上循环的 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);
}
或者您可以为每条现有路线创建折线。
我正在使用 waypoints 在 google 地图上绘制路线。一切看起来都很好,除了我的出发地和目的地也相连。我确定我的出发地和目的地不一样。
这是我从 latitude/longitudes 的列表生成路线 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);
}
Activity 与地图对象 -
@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));
}
我认为,问题在于您将所有可能的路线添加到同一条折线。因此,多段线走第一条路线,然后 "returns" 到起点,然后再走第二条路线。您要么需要删除在结果上循环的 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);
}
或者您可以为每条现有路线创建折线。