在谷歌地图中绘制多边形线时避免一个点(LatLng)

时间:2017-04-19 08:08:54

标签: java android google-maps google-maps-api-3 maps

我从webservice获取纬度和经度值,我将它们存储在arraylist中。从arraylist我需要绘制具有某些条件的折线。条件是两个lat,long值之间的差值是否大于一米。我需要避免那个特别的纬度很长的值。我使用了以下代码段。

段:

    private void redrawLine()
    {

    mMap.clear();  //clears all Markers and Polylines

    PolylineOptions options = new PolylineOptions().width(3).color(Color.BLUE).geodesic(true);
    for (int i = 0; i < coordList.size(); i++) {
    point = coordList.get(i);
      }
    for(int j=1; j<coordList.size();j++){
        points = coordList.get(j);
    }
    CalculationByDistance(point,points);
    int m = meterInDec*1000;
    if(m>1){
        Toast.makeText(MapsActivity.this, String.valueOf(m), Toast.LENGTH_SHORT).show();   //do nothing
    }
    else{
        options.add(point);   //add points
    }
   // addMarker(); //add Marker in current position
    line = mMap.addPolyline(options); //add Polyline
}

方法CalculationByDistance

    public double CalculationByDistance(LatLng StartP, LatLng EndP) {
    int Radius = 6371;// radius of earth in Km
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    meterInDec = Integer.valueOf(newFormat.format(meter));
    Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
            + " Meter   " + meterInDec);

    return Radius * c;
}

使用这些条件时。我无法划线。但如果没有条件,它就可以了。

1 个答案:

答案 0 :(得分:0)

当您为点和点变量赋值时,只会将coordList中的最后一条记录分配给它们。您将要进行的计算将仅基于最后一条记录。

for (int i = 0; i < coordList.size(); i++) {
    point = coordList.get(i);
}
for(int j=1; j<coordList.size();j++){
    points = coordList.get(j);
}
CalculationByDistance(point,points);

您需要在循环内移动距离计算或更改redrawLine方法的逻辑