2013-08-26 65 views
1

我在谷歌地圖上繪製了多個使用多個緯度,經度points.But多邊形,但現在我需要在多邊形的中心放置一個標記,我需要中心座標。如何計算中心點。如何計算Google Maps Android API v2中多邊形的中心?

下面是在地圖上添加多邊形代碼:

for (Warning w : warningsList) { 
      // Instantiates a new Polygon object and adds points 
      PolygonOptions rectOptions = new PolygonOptions(); 
      List<PolyPoints> pp = w.getPolyPoints(); 
      for (PolyPoints p : pp) { 
       rectOptions.add(new LatLng(Double.valueOf(p.getLatitude()), 
         Double.valueOf(p.getLongitude()))); 
      } 

       mMap.addPolygon(rectOptions.strokeColor(Color.GREEN) 
         .fillColor(Color.RED).strokeWidth(STROKE_WIDTH)); 

     } 

我發現了類似question這已經回答了,但對JavaScript這就是Api.Is他們用我的情況下,同一個解決方案的方法嗎?

+0

有沒有可以提供給我們的任何代碼? – ksugiarto

+0

@kolonelpeteruk我已經添加了一些代碼,顯示了我如何在地圖上添加多邊形。 – user818455

+0

你想實施什麼樣的中心?重心?邊界的中心?你嘗試過哪一個實施? –

回答

10

下面是我使用的,現在找到多邊形的中心代碼: -

public static double[] centroid(List<PolyPoints> points) { 
     double[] centroid = { 0.0, 0.0 }; 

     for (int i = 0; i < points.size(); i++) { 
      centroid[0] += points.get(i).getLatitude(); 
      centroid[1] += points.get(i).getLongitude(); 
     } 

     int totalPoints = points.size(); 
     centroid[0] = centroid[0]/totalPoints; 
     centroid[1] = centroid[1]/totalPoints; 

     return centroid; 
    } 
14

下面的代碼我使用找到多邊形的中心點。它的工作對我來說太

private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){ 
     LatLng centerLatLng = null; 
     Builder builder = new LatLngBounds.Builder(); 
     for(int i = 0 ; i < polygonPointsList.size() ; i++) 
     {   
      builder.include(polygonPointsList.get(i)); 
     } 
     LatLngBounds bounds = builder.build(); 
     centerLatLng = bounds.getCenter(); 

     return centerLatLng; 
    } 
1
Builder builder = new LatLngBounds.Builder(); 
0
private static LatLng getCenterOfPolygon(List<LatLng> latLngList) { 
     double[] centroid = {0.0, 0.0}; 
     for (int i = 0; i < latLngList.size(); i++) { 
      centroid[0] += latLngList.get(i).latitude; 
      centroid[1] += latLngList.get(i).longitude; 
     } 
     int totalPoints = latLngList.size(); 
     return new LatLng(centroid[0]/totalPoints, centroid[1]/totalPoints); 
    } 
1

我已經使用兩種方法做到了。但它適用於任何形狀......

private static double area(ArrayList<LatLng> arr) { 
    double area=0; 
    int nPts = arr.size(); 
    int j=nPts-1; 
    LatLng p1; LatLng p2; 
    for (int i=0;i<nPts;j=i++) { 

     p1=arr.get(i); p2=arr.get(j); 
     area+=p1.latitude*p2.longitude; 
     area-=p1.longitude*p2.latitude; 
    } 
    area/=2; 

    return area; 
}; 

public static LatLng Centroid (ArrayList<LatLng> pts) { 
    int nPts = pts.size(); 
    double x=0; double y=0; 
    double f; 
    int j=nPts-1; 
    LatLng p1; LatLng p2; 

    for (int i=0;i<nPts;j=i++) { 
     p1=pts.get((i)); p2=pts.get(j); 
     f=p1.latitude*p2.longitude-p2.latitude*p1.longitude; 
     x+=(p1.latitude+p2.latitude)*f; 
     y+=(p1.longitude+p2.longitude)*f; 
    } 

    f=area(pts)*6; 

    return new LatLng(x/f, y/f); 
}; 

簡單的調用類中重心方法,並通過在您的經緯度數組作爲參數。確保你傳遞的Array列表是LatLng Array List

LatLng polyCentroid = Centroid(list);

mMap.addMarker(new MarkerOptions() .position(polyCentroid));

相關問題