2017-04-09 70 views
0

我是Android應用開發新手。 我有WKT(POLYGON) 如何從wkt在Google地圖上繪製多邊形?Android。谷歌地圖。如何從數組中繪製多邊形

我嘗試

String str; 

     ArrayList<String> coordinates = new ArrayList<String>(); 

     str = tvwkt.getText().toString(); 

     str = str.replaceAll("\\(", ""); 
     str = str.replaceAll("\\)", ""); 
     str = str.replaceAll("POLYGON", ""); 
     str = str.replaceAll("POINT", ""); 
     str = str.replaceAll(", ", ","); 
     str = str.replaceAll(" ", ","); 
     str = str.replaceAll(",,", ","); 


     String[] commatokens = str.split(","); 
      for (String commatoken : commatokens) { 
       coordinates.add(commatoken); 
     } 

     for (int i = 0; i < coordinates.size(); i++) { 

      String[] tokens = coordinates.get(i).split("\\s"); 
      for (String token : tokens) { 

       listPoints.add(token); 
      } 

     } 

     PolygonOptions rectOptions = new PolygonOptions().addAll(listPoints).strokeColor(Color.BLUE).fillColor(Color.CYAN).strokeWidth(7); 

     polygon = mMap.addPolygon(rectOptions); 

但它不能正常工作。 Hepl我請。 謝謝。

+0

能否請您發表你得到的輸出,或者更具體幹了什麼不行? – Loren

回答

0

我可以做到。

閱讀的LatLong從WKT並添加到陣列

private LatLng[] GetPolygonPoints(String polygonWkt) { 

    Bundle bundle = getIntent().getExtras(); 
    wkt = bundle.getString("wkt"); 
    ArrayList<LatLng> points = new ArrayList<LatLng>(); 
    Pattern p = Pattern.compile("(\\d*\\.\\d+)\\s(\\d*\\.\\d+)"); 
    Matcher m = p.matcher(wkt); 
    String point; 

    while (m.find()){ 
     point = wkt.substring(m.start(), m.end()); 
     points.add(new LatLng(Double.parseDouble(m.group(1)), Double.parseDouble(m.group(2)))); 
    } 
    return points.toArray(new LatLng[points.size()]); 

} 

然後繪製多邊形

public void Draw_Polygon() { 

    LatLng[] points = GetPolygonPoints(polygonWkt); 

     Polygon p = mMap.addPolygon(
       new PolygonOptions() 
         .add(points) 
         .strokeWidth(7) 
         .fillColor(Color.CYAN) 
         .strokeColor(Color.BLUE) 
     ); 

    //Calculate the markers to get their position 
    LatLngBounds.Builder b = new LatLngBounds.Builder(); 
    for (LatLng point : points) { 
     b.include(point); 
    } 
    LatLngBounds bounds = b.build(); 
    //Change the padding as per needed 
    CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, 20,20,5); 
    mMap.animateCamera(cu); 

} 

終於

public void onMapReady(GoogleMap googleMap) { 

    mMap = googleMap; 

    mMap.setMapType(MAP_TYPE_HYBRID); 

    mMap.getUiSettings().setRotateGesturesEnabled(false); 

    mMap.getUiSettings().setMapToolbarEnabled(false); 

    LatLng[] points = GetPolygonPoints(polygonWkt); 

    if (points.length >3){ 

     Draw_Polygon(); 

    } 
    else { 

     Add_Markers(); 

    } 

} 
相關問題