【问题标题】:I am getting the LatLng points but the polyline is not showing on the map我正在获取 LatLng 点,但折线未显示在地图上
【发布时间】:2015-04-06 06:13:37
【问题描述】:

我正在获取 LatLng 值并将所有这些 latlng 放入 arraylist(points),当我调试代码时,我可以看到 latlng 点正在进入 arraylist,但我没有在地图上获得折线。先感谢您。

private BroadcastReceiver receiver = new BroadcastReceiver() {
    @Override 
    public void onReceive(Context context, Intent intent) {
        if (intent.getExtras() != null ) {
            points=new ArrayList<LatLng>();
            Lat=bundle.getDouble("latitude", 0d);
            Lng=bundle.getDouble("longitude", 0d);
                       Toast.makeText(getApplicationContext(),""+Lat+","+Lng,Toast.LENGTH_SHORT).show()‌​;
            point = new LatLng(Lat,Lng);
            PolylineOptions polylineOptions = new PolylineOptions();
            polylineOptions.color(Color.BLUE);
            polylineOptions.width(8);
            points.add(point);
            polylineOptions.addAll(points);
            googleMap.addPolyline(polylineOptions);
        }
    }
};

【问题讨论】:

  • 你会改变 polylineOptions.addAll(points);用for循环试试? for(int i=0;i
  • 试试这个示例androidhub4you.com/2013/07/…
  • 我猜你是在循环中执行这段代码,你在点数组列表中添加点,所以只需将最后两行移出循环,你就会在地图中绘制所有折线。最后两行` polylineOptions.addAll(points); googleMap.addPolyline(polylineOptions);`
  • @Clairvoyant - 我将那两条线放在循环之外,但我也没有得到折线。
  • @RagamaiK - 你能发布你的完整代码以正确的方式帮助你,而不是猜测和回答你。

标签: java android


【解决方案1】:

在调用广播接收器时不要重新创建数组列表。 希望对您有所帮助。

public class MainActivity extends Activity {
  ArrayList<LatLng> points=new ArrayList<LatLng>();
 private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override 
public void onReceive(Context context, Intent intent) {
    if (intent.getExtras() != null ) {

        Lat=bundle.getDouble("latitude", 0d);
        Lng=bundle.getDouble("longitude", 0d);
                   Toast.makeText(getApplicationContext(),""+Lat+","+Lng,Toast.LENGTH_SHORT).show()‌​;
        point = new LatLng(Lat,Lng);
        PolylineOptions polylineOptions = new PolylineOptions();
        polylineOptions.color(Color.BLUE);
        polylineOptions.width(8);
        points.add(point);
        polylineOptions.addAll(points);
        googleMap.addPolyline(polylineOptions);
    }
  }
 };
}

【讨论】:

  • 如果这能解决您的问题,请接受这个答案。
猜你喜欢
  • 2014-08-03
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多