在Android V2中使用谷歌地图API绘制两个位置之间的路线

9
我在安卓上尝试使用Google Maps API V2,试图获取两个位置之间的路径,并使用JSON解析实现此目标。
我能获得一条路线,并且起始路线是正确的。但是在某个点上它走错了方向。我的终点位置也有误。对于其他一些位置,我的应用程序直接崩溃。
这是我所做的:
这是我的makeURL方法:
public String makeUrl(){
    StringBuilder urlString = new StringBuilder();
    urlString.append("http://maps.googleapis.com/maps/api/directions/json");
    urlString.append("?origin="); //start positie
    urlString.append(Double.toString(source.latitude));
    urlString.append(",");
    urlString.append(Double.toString(source.longitude));
    urlString.append("&destination="); //eind positie
    urlString.append(Double.toString(dest.latitude));
    urlString.append(",");
    urlString.append(Double.toString(dest.longitude));
    urlString.append("&sensor=false&mode=driving");

    return urlString.toString();
}

我的JSON解析器

public class JSONParser {

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {
    // TODO Auto-generated constructor stub
}

public String getJSONFromURL(String url){

    try {
        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) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        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");
            //Log.e("test: ", sb.toString());
        }

        json = sb.toString();
        is.close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        Log.e("buffer error", "Error converting result " + e.toString());
    }

    return json;
}

我用这种方法绘制我的路径。
public void drawPath(String result){
    try{
        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");
        Log.d("test: ", encodedString);
        List<LatLng> list = decodePoly(encodedString);

        LatLng last = null;
        for (int i = 0; i < list.size()-1; i++) {
            LatLng src = list.get(i);
            LatLng dest = list.get(i+1);
            last = dest;
            Log.d("Last latLng:", last.latitude + ", " + last.longitude );
            Polyline line = googleMap.addPolyline(new PolylineOptions().add( 
                    new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
                    .width(2)
                    .color(Color.BLUE));
        }

        Log.d("Last latLng:", last.latitude + ", " + last.longitude );
    }catch (JSONException e){
        e.printStackTrace();
    }
}

我使用以下方法解析JSON:

private List<LatLng> decodePoly(String encoded){

    List<LatLng> poly = new ArrayList<LatLng>();
    int index = 0;
    int length = encoded.length();
    int latitude = 0;
    int longitude = 0;

    while(index < length){
        int b;
        int shift = 0;
        int result = 0;

        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);

        int destLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        latitude += destLat;

        shift = 0;
        result = 0;
        do {
            b = encoded.charAt(index++) - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b > 0x20);

        int destLong = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
        longitude += destLong;

        poly.add(new LatLng((latitude / 1E5),(longitude / 1E5) ));
    }
    return poly;
}

然后使用AsyncTask进行编码。

提前表示感谢。

3个回答

2

抱歉让您等待这么久,我之前就已经修复了,只是还没有在这里发布我的解决方案。

问题基本上是个打字错误...

在我的Json解码器中,我使用了两个Do while语句。

while (b >= 0x20);

在第二个“Do While”语句中,我忘记了“=”符号。 因此它没有正确地呈现...... 谢谢。

1
我相信您正在从overview_polyline创建您的LatLng对象。根据Google文档,这个对象包含一个编码点数组,表示结果方向的近似(平滑)路径。

我很确定您可以通过基于legs[]steps[]数据构建LatLng对象来获得更详细的路线,因为官方文档指出:一步是方向路线的最小单元,包含描述旅程上特定单个指令的单个步骤

请看:

https://developers.google.com/maps/documentation/directions/#Routes


0
Tmichel,Michael 有正确的波浪线,因为腿部和步伐在您的路线中绘制了离开街道的直线。腿部和步伐提供了关于坐标的信息,以便向用户发出警报。 折线是街道上正确而精确的点。对不起我的糟糕英语。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接