2013-10-21 64 views
14

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

问题是我想不出一个很好的解决方案,我不知道是否有可能。如果我将多段线的坐标数组传递给方向服务路线,则它只会使用多段线的起点和终点绘制一条路线,而不考虑其间的任何坐标。

我试图生成使用折线一个“航点”阵列由均匀划分它和在之间并在消逝那些作为沿途停车点获得8个坐标的坐标阵列,但它未能现在在所有渲染。如果我使用通过绘制路线生成的坐标数组来测试代码,它可以工作,所以我知道代码正在工作。我假设它失败了,因为其中一些坐标可能稍微偏离了道路(这是从GPS定位中绘制的多段线,因此它不是100%准确的),Google不会将它捕捉到最近的可接受位置。

任何人都可以想到一个解决方案吗?

下面的代码实例,使之更清楚一点:

// 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); 
    } 
}); 
+0

我有一些想法,但我需要数据来测试。你能发布你正在使用的坐标数组吗? –

+0

您是否尝试过“optimizeWaypoints”标志? –

+0

我没有托马斯,但根据选项的描述,它不会很理想,因为它会严重重新排列路线。司机有时会采取非效率的方式来避开某些道路/桥梁等。 乍得,抱歉,但我找不到任何好的例子给你,我失去了测试数据。 – andro1d

回答

0
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); 
      } 
     } 
    } 
2

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

地图API不适用于这种用途的情况下,有几个好甚至没有尝试的原因:

  • Waypoint无论前一个/下一个航点是什么,停车(默认)都会允许任何行驶方向,无论是上一个/下一个航点。

  • 航点未停过(通过:)将捕捉到的道路的时候非常严格,一般的GPS偏移会抛出其关闭,并导致ZERO_RESULTS(没有路由) -

  • 即使所有航点工作那么路线将成为通用驾驶员的最佳路线,而不一定是采样用作路标的位置的车辆的路线。

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