首页 文章

Google Maps Utils如何解码列表中的折线值?

提问于
浏览
2

我正在使用谷歌 Map 方向Api和谷歌 Map 实用程序库绘制我的位置与标记之间的路径,我从方向Api检索字段overview-polyline,但绘制的折线不正确所以现在我'm trying to build the polyline step by step, but I'米面对一个问题,我能够将所有折线添加到arraylist但现在如何从arraylist中检索它们以解码并构建它们;

这是JSON:

{
geocoded_waypoints: [
{},
{}
],
routes: [
{
bounds: {},
copyrights: "Dados do mapa ©2017 Google, Inst. Geogr. Nacional",
legs: [
{
distance: {},
duration: {},
end_address: "14 Avenue Latécoère, 31700 Cornebarrieu, França",
end_location: {},
start_address: "R. Zeferino Brandão 9, 2005 Santarém, Portugal",
start_location: {},
steps: [
{
distance: {},
duration: {},
end_location: {},
html_instructions: "Siga para <b>noroeste</b> na <b>R. Zeferino Brandão</b> em direção a <b>Tv. de São Domingos</b>",
polyline: {
points: "k|nnF`p_t@GB{@t@MFQPMLKXETEZCVCL?@?BAD?@?DAFC|@"
},
start_location: {},
travel_mode: "DRIVING"
},
{},

这是我解码overview-polyline的方式:

...
JSONObject singleRout = (JSONObject) routes.get(0);
    JSONObject overview_polyline = (JSONObject) singleRout.get("overview_polyline");
        if (overview_polyline != null) {
            points = overview_polyline.getString("points");
        }
...

protected void fazerCaminho() {
    List<LatLng> decodedPath = PolyUtil.decode(points);

    if (line == null) {
        line = mMap.addPolyline(new PolylineOptions()
                .width(3)
                .color(Color.rgb(25, 151, 152))
                .geodesic(true)
                .addAll(decodedPath));
    } else {
        line.remove();
        line = mMap.addPolyline(new PolylineOptions()
                .width(3)
                .color(Color.rgb(25, 151, 152))
                .geodesic(true)
                .addAll(decodedPath));
    }
}

现在我将所有的点添加到arraylist中,如下所示:

JSONObject singlePolyline = (JSONObject) singleStep.get("polyline");
    if (singlePolyline != null) {
        points = singlePolyline.getString("points");
        caminho.put("points" , points);
    }

    listaPolylines.add(caminho);

如何解码列表中的值?

2 回答

  • -1

    您可以使用Google Maps Android API Utility Library中的 PolyUtil.decode 方法:

    List<LatLng> decoded = PolyUtil.decode(caminho.get("points"));
    

    在您的情况下,如果您想从 listaPolylines 解码:

    for (Map polyline : listaPolylines) {
        List<LatLng> decoded = PolyUtil.decode(polyline.get("points"));
    
        // Do something with your decoded polyline. For example drawing it on your map
        mMap.addPolyline(new PolylineOptions().addAll(decoded));
    }
    
  • 10

    创建自定义模型,并创建该自定义模型的数组列表以存储LatLng .

    如下所示:

    public class CustomLocations{
    private LatLng latLng;
    public customLocations(LatLng latLng){
    this.latLng = latLng
    }
    public void setLatLng(LatLng latLng){
    this.latLng = latLng
    }
    public LatLng getLatLng(){
    return latLng;
    }
    }
    
    //Now, create an array list:
    ArrayList<CustomLocations> yourArray = new ArrayList<>();
    
    //add locations:
    yourArray.add(new CustomLocations(yourLatLng));
    
    //retrieve:
    yourArray.get(i).getLatLng();
    

相关问题