在地图视图叠加使用不同的颜色路径段 [英] Use different color for path segment in the map view overlay
问题描述
我的应用程序可以使用JSON给出的路径/方向。我怎样才能在不同的颜色改变由JSON给定的路径?
My application can give a path/direction using JSON. How can I change in different colors in on path given by JSON?
例如,形式A点到B点为红色,B到C是黄色,C到D为绿色,等等。
For example, form point A to point B is red, B to C is yellow, C to D is green, and so on.
在code是:
获取点JSON。
private void parsing(GeoPoint start, GeoPoint end) throws ClientProtocolException, IOException, JSONException, URISyntaxException{
HttpClient httpclient = new DefaultHttpClient();
StringBuilder urlstring = new StringBuilder();
urlstring.append("https://maps.googleapis.com/maps/api/directions/json?origin=")
.append(Double.toString((double)start.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)start.getLongitudeE6()/1E6)).append("&destination=")
.append(Double.toString((double)end.getLatitudeE6()/1E6)).append(",").append(Double.toString((double)end.getLongitudeE6()/1E6))
.append("&sensor=false");
//urlstring.append("http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=true");
url = new URI(urlstring.toString());
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = null;
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
reader.close();
String result = sb.toString();
JSONObject jsonObject = new JSONObject(result);
JSONArray routeArray = jsonObject.getJSONArray("routes");
JSONObject routes = routeArray.getJSONObject(0);
JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
List<GeoPoint> pointToDraw = decodePoly(encodedString);
//Added line:
mv_mapview.getOverlays().add(new RoutePathOverlay(pointToDraw));
}
由JSON给出Geopoints列表。
List of Geopoints given by JSON.
private List<GeoPoint> decodePoly(String encoded) {
List<GeoPoint> poly = new ArrayList<GeoPoint>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((int) (((double) lat / 1E5) * 1E6), (int) (((double) lng / 1E5) * 1E6));
//n = new Node(p,);
poly.add(p);
}
return poly;
}
和叠加类
public class RoutePathOverlay extends Overlay {
private int _pathColor;
private final List<GeoPoint> _points;
private boolean _drawStartEnd;
public RoutePathOverlay(List<GeoPoint> points) {
this(points, Color.GREEN, true);
}
public RoutePathOverlay(List<GeoPoint> points, int pathColor, boolean drawStartEnd) {
_points = points;
_pathColor = pathColor;
_drawStartEnd = drawStartEnd;
}
private void drawOval(Canvas canvas, Paint paint, Point point) {
Paint ovalPaint = new Paint(paint);
ovalPaint.setStyle(Paint.Style.FILL_AND_STROKE);
ovalPaint.setStrokeWidth(2);
int _radius = 6;
RectF oval = new RectF(point.x - _radius, point.y - _radius, point.x + _radius, point.y + _radius);
canvas.drawOval(oval, ovalPaint);
}
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Projection projection = mapView.getProjection();
if (shadow == false && _points != null) {
Point startPoint = null, endPoint = null;
Path path = new Path();
//We are creating the path
for (int i = 0; i < _points.size(); i++) {
GeoPoint gPointA = _points.get(i);
Point pointA = new Point();
projection.toPixels(gPointA, pointA);
//if()
if (i == 0) { //This is the start point
startPoint = pointA;
path.moveTo(pointA.x, pointA.y);
} else {
if (i == _points.size() - 1)//This is the end point
endPoint = pointA;
path.lineTo(pointA.x, pointA.y);
}
}
Paint paint = new Paint();
paint.setAntiAlias(true);
// if(){
paint.setColor(_pathColor);
//}
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setAlpha(90);
if (getDrawStartEnd()) {
if (startPoint != null) {
drawOval(canvas, paint, startPoint);
}
if (endPoint != null) {
drawOval(canvas, paint, endPoint);
}
}
if (!path.isEmpty())
canvas.drawPath(path, paint);
}
return super.draw(canvas, mapView, shadow, when);
}
public boolean getDrawStartEnd() {
return _drawStartEnd;
}
public void setDrawStartEnd(boolean markStartEnd) {
_drawStartEnd = markStartEnd;
}
}
推荐答案
您仅解码 overview_polyline
。
此行的每一部分(步骤
响应JSON数组)被定义为
Each part of the trip (steps
array in response JSON) is defined as
{
distance: {
text: "89 m",
value: 89
},
duration: {
text: "1 min",
value: 8
},
end_location: {
lat: 45.51101000000001,
lng: -73.5545
},
html_instructions: "Enter <b>Rue Saint Antoine E</b>",
polyline: {
points: "cvwtG~d}_MaBs@s@W"
},
start_location: {
lat: 45.51026,
lng: -73.55488000000001
},
travel_mode: "DRIVING"
},
正如你可以看到有每个步骤的折线。您是否尝试解码单步,将它们存储在一个数组,然后想象每个部分(使用不同的颜色为旅行的每个部分)?
这会给你一个行程的部门的决议,但它可能是一个很好的起点。
As you can see there is a polyline for each step. Have you tried decoding single steps, store them in an array and then visualize each section (using a different color for each part of the trip)? This will give you a resolution of a "sector" of the trip, but it could be a good start point.
或者你可以尝试生成 overview_polyline
的每一个secton数组(因为它由点定义你可以创建单行:如果你的折线通过槽A,B, C和D可以定义3段AB BC CD)。
OR you could try to generate an array for every secton of overview_polyline
(since its defined by points you could create single lines: if your polyline passes trough A, B, C and D you can define 3 segments A-B B-C C-D).
我不知道多少,这(产生和可视化有很多在这件覆盖)会影响性能,因为我从来没有使用地图API以这种方式(我用它来获得ETA一段跳闸),所以我'M只是猜测:D
I don't know how much this (generate and visualize an overlay with a lot of pieces in it) will affect performance since i never used Maps API in this way (i used it to get ETA for some trip) so i'm just guessing :D
这篇关于在地图视图叠加使用不同的颜色路径段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!