Polyline not visible Android Maps Api v2

强颜欢笑 提交于 2019-12-08 09:33:34

问题


I am a little bit confused since everything seems to be alright with the code. The problem is that the polyline won't show up on the map.

Here is the function that I call to place polyline everytime I receive a location

(I added markers in a similar way and they work great)

private void addPolylineLocationOnMap(LatLng newLoc)
{
    PolylineOptions poly = new PolylineOptions()
                                    .add(newLoc)
                                    .color(Color.BLUE)
                                    .width(5)
                                    .visible(true)
                                    .zIndex(30);

    googleMap.addPolyline(poly);
}

回答1:


A Polyline needs multiple points!

For example, pass an ArrayList<LatLng> to your method and use addAll() rather than just add().

From the PolylineOptions documentation:

add(LatLng... points) : Adds vertices to the end of the polyline being built.

Alternatively, you can keep a reference to one Polyline and use add() to add points to it as you receive them.

Add poly as an instance variable in your class:

PolylineOptions poly;

Then in onCreate() (or wherever you set up the map):

poly = new PolylineOptions()
    .color(Color.BLUE)
    .width(5)
    .visible(true)
    .zIndex(30);

googleMap.addPolyline(poly);

Then as you receive more points:

poly.add(newLoc);


来源:https://stackoverflow.com/questions/22516015/polyline-not-visible-android-maps-api-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!