将折线转换为路线

时间:2013-10-21 23:28:08

标签: google-maps google-maps-api-3

我有一个应用程序跟踪车辆并在地图上绘制他们的旅行路径折线。我想使用路线服务路线将此折线转换为路线。这将允许我能够拖动路径并操纵它等。

问题是我想不出一个很好的解决方案,而且我不确定它是否可行。如果我将折线的坐标数组传递到路线服务路线,它只使用折线的起点和终点绘制路线,它不会考虑其间的任何坐标。

我尝试使用折线坐标数组生成'waypoints'数组,方法是将其均匀分割并在其间获得8个坐标并将其作为路点传递,但现在根本无法渲染。如果我使用通过绘制路由生成的坐标数组来测试代码,那么我知道代码正常工作。我认为它失败了,因为其中一些坐标可能稍微不在路上(它是从GPS定位绘制的折线,所以它不是100%准确),而谷歌不只是将它捕捉到最近的接受位置。

有人能想到解决方案吗?

以下代码示例使其更清晰:

// In the polyline app
var encoded_path = google.maps.geometry.encoding.encodePath(coordinate_array)



// In the route app
var coordinates = google.maps.geometry.encoding.decodePath(encoded_path);

var waypoints = [];

// Evenly get coordinates across the entire array to be used as waypoints
for (var i = 1; i <= 8; ++i) {
   var index = Math.floor((coordinates.length/10) * i);

   if (index >= coordinates.length - 1)
      break;

   waypoints.push({
      'location': new google.maps.LatLng(coordinates[index].lat(), coordinates[index].lng()),
      'stopover': false
   });
}

var request = {
   origin: coordinates[0],
   destination: coordinates[coordinates.length - 1],
   travelMode: google.maps.DirectionsTravelMode.DRIVING,
   waypoints: waypoints
};

MapService.directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
      MapService.directionsDisplay.setDirections(response);
   }
});

3 个答案:

答案 0 :(得分:2)

现在已经有一段时间了,现在有了更好的答案,Roads API: https://developers.google.com/maps/documentation/roads/intro

Directions API不适用于此用例,有几个很好的理由甚至不尝试:

  • 停留的路标(默认)将允许在捕捉到最近的道路时进出的任何行进方向,无论前一个/下一个航点如何。

  • 没有停留的路标(通过:)在捕捉到道路时非常严格,典型的GPS偏移会将其抛弃并导致ZERO_RESULTS(无路线) -

  • 即使所有航路点都运行良好,路线也是通用驾驶员的最佳路线,不一定是车辆采用路线作为航路点的路线。

  • 如果车辆在不同高度的2条道路(高架,桥梁,隧道等)的交叉点处采样位置,如果GPS偏移使得该点位于错误的道路上,则可以抛出路线疯狂地离开。

答案 1 :(得分:0)

您可以使用方向api检查道路结束时的航点:Map of all points below a certain time of travel?。然后删除其他人以从整个折线创建路线。

答案 2 :(得分:-1)

public String makeURL (double sourcelat, double sourcelog, double destlat, double destlog ){
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin=");// from
    urlString.append(Double.toString(sourcelat));
    urlString.append(",");
    urlString
            .append(Double.toString( sourcelog));
    urlString.append("&destination=");// to
    urlString
            .append(Double.toString( destlat));
    urlString.append(",");
    urlString.append(Double.toString( destlog));
    urlString.append("&sensor=false&mode=driving");
    return urlString.toString();

}

private List<LatLng> decodePoly(String encoded) {

    List<LatLng> poly = new ArrayList<LatLng>();
    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;

        LatLng p = new LatLng( (((double) lat / 1E5)),
                 (((double) lng / 1E5) ));
        poly.add(p);
    }

    return poly;
}


public class JSONParser {

    InputStream is = null;
    JSONObject jObj = null;
    String json = "";
    // constructor
    public JSONParser() {
    }
    public String getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            json = sb.toString();
            is.close();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        return json;

    }
}

public void drawPath(String  result) {

    try {
            //Tranform the string into a json object
           final JSONObject json = new JSONObject(result);
           JSONArray routeArray = json.getJSONArray("routes");
           JSONObject routes = routeArray.getJSONObject(0);
           JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
           String encodedString = overviewPolylines.getString("points");
           List<LatLng> list = decodePoly(encodedString);

           for(int z = 0; z<list.size()-1;z++){
                LatLng src= list.get(z);
                LatLng dest= list.get(z+1);
                theMap.addPolyline(new PolylineOptions()
                .add(src,dest)
                .width(2)
                .color(Color.BLUE).geodesic(true));
            }

    } 
    catch (JSONException e) {

    }
} 


private class connectAsyncTask extends AsyncTask<Void, Void, String>{
        private ProgressDialog progressDialog;
        String url;
        connectAsyncTask(String urlPass){
            url = urlPass;

        }
        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog = new ProgressDialog(YOUR_Activity.this);
            progressDialog.setMessage("Fetching route, Please wait...");
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }
        @Override
        protected String doInBackground(Void... params) {
            JSONParser jParser = new JSONParser();
            String json = jParser.getJSONFromUrl(url);
            return json;
        }
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);   
            progressDialog.hide();        
            if(result!=null){
                drawPath(result);
            }
        }
    }