0

我的应用程序可以使用 JSON 提供路径/方向。如何在 JSON 给出的路径中更改不同的颜色?

例如,从 A 点到 B 点为红色,B 到 C 为黄色,C 到 D 为绿色,以此类推。

代码是:

在 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 给出的地理点列表。

    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;
}

}

4

1 回答 1

0

你只是在解码overview_polyline

行程的每一部分(steps响应 JSON 中的数组)定义为

{
    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"
},

如您所见,每个步骤都有一条折线。您是否尝试过解码单个步骤,将它们存储在一个数组中,然后可视化每个部分(为旅行的每个部分使用不同的颜色)?这将为您提供旅行“部门”的解决方案,但这可能是一个很好的起点。

或者您可以尝试为每个部分生成一个数组overview_polyline(因为它由点定义,您可以创建单线:如果您的折线通过 A、B、C 和 D 槽,您可以定义 3 段 AB BC CD)。

我不知道这(生成并可视化包含很多片段的叠加层)会影响性能,因为我从未以这种方式使用过 Maps API(我用它来获取 ETA 进行一些旅行)所以我只是猜测:D

于 2012-11-22T08:47:55.477 回答