2017-04-09 2 views
0

Je suis nouveau dans le développement d'applications Android. J'ai WKT (POLYGON) Comment dessiner un polygone sur google map depuis wkt?Android. Google Map. Comment dessiner un polygone à partir du tableau

J'essaie

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); 

Mais son travail ne. Hepl moi s'il vous plaît. merci.

+0

Pouvez-vous s'il vous plaît poster la sortie que vous avez, ou plus précisément ce qui a fait ne fonctionne pas? – Loren

Répondre

0

Je peux le faire.

Lire LatLong de WKT et ajouter à tableau

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()]); 

} 

puis dessiner un polygone

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); 

} 

enfin

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(); 

    } 

}