2012-09-11 96 views
2

我有這種格式的數據:如何處理Android中的WKT數據?

POINT(73.0166738279393 33.6788721326803) 
MULTILINESTRING((73.0131224998036 33.679001500419,73.0119635003153 33.678392400389,73.0119205001311 33.6783781002692),(73.0131224998036 33.679001500419,73.0136031002029 33.6783443999742),(73.0136031002029 33.6783443999742,73.0147099372139 33.67685138958),(73.0147099372139 33.67685138958,73.0150124997272 33.6770292997624,73.0154158996241 33.6773507003746,73.0157677998441 33.6776577999676,73.016042399737 33.6779721004322,73.0162998999205 33.6783149004124,73.0166738279393 33.6788721326803)) 

現在我要畫它在谷歌地圖中的Android。我製作了一組名稱和座標。

ArrayList<String> coordinates = new ArrayList<String> 
coordinates.add(tvcoor.getText().toString()); 

這會產生一個錯誤:當我運行我的應用程序時,它強制停止。 如何在地圖上繪製它?從TextView的

+1

請發表您的logcat – rajeshwaran

+0

吉爾斯我添加if語句,因爲TextView的還收到另一個數據。但我想只有座標數據存儲到數組列表 –

+0

感謝我有ATLEAST解決我的錯誤 –

回答

2

試試這個,

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

首先得到的字符串。

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

第二次刪除括號。

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

然後用逗號分開並添加值的ArrayList。

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

然後我們在索引位置獲得獨立的座標值,

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

      String[] tokens = coordinates.get(i).split("\\s"); 
      for (String token : tokens) { 
       System.out.println("-" + token + "-"); 
      } 
     } 
+0

感謝rajesh的幫助,但我無法理解最後一步,當我發送這個coordiantes arraylist的mapactivity,然後從mapactivity覆蓋類,並畫出一個點 –

+0

Intent in = getIntent(); loc = in.getStringArrayListExtra(「stringname」); mapOverlays = mapView.getOverlays(); proj = mapView.getProjection(); overlay = new MyOverlay(locations);和繪製這樣公共MyOverlay一個點(列表位置){ \t \t // TODO自動生成構造存根 \t \t 的GeoPoint熔點=新的GeoPoint((int)的(的Integer.parseInt(locations.get(0)) * 1E6),(int)(Integer.parseInt(locations.get(1))* 1E6));然後在應用程序停止意外 –

+0

它具有在將其保存[0] = POINT(73.0166738279393 33.6788721326803) MULTILINESTRING第一位置的一些問題((73.0131224998036 33.679001500419; [1] = 73.0119635003153 33.678392400389;然後再次[2] = POINT(73.0166738279393 33.6788721326803) MULTILINESTRING((73.0131224998036 33.679001500419; –

1

對於其他人誰都有這種相同的情況下,我找到了被稱爲JTS Topology Suite一個開源庫,其中有解析WKT字符串的能力在Java中。從我的應用程序一個基本的例子是這樣的:

WKTReader wktReader = new WKTReader(); 
Geometry geometry = wktReader.read(routeResponse.get(yourWKTMultilineString); 

然後,您可以通過單獨的線,像這樣重複:

for(int lineIndex = 0; lineIndex < geometry.getNumGeometries(); lineIndex++){ 
    Geometry lineGeometry = geometry.getGeometryN(lineIndex); 
    //... other stuff 
} 

如果需要,您可以得到像這樣每行的各個座標:

Coordinate[] lineCoordinates = lineGeometry.getCoordinates(); 

請注意,以上是一個非常一般的例子,它遵循OP的代碼。

最終,這可能會使其他人無需推出自己的WKT解析器。


Nutiteq特定的代碼:
以我的情況下,我需要繪製一個多行字符串作爲一個Nutiteq map的載體層。我意識到OP問的是谷歌地圖Android的API,但在情況下,任何閱讀器也使用Nutiteq(或者,如果該算法相關的其他地圖的API),這是nutiteq具體代碼:

geomLayer.clear(); // clear the old vector data 

Projection projection = geomLayer.getProjection(); // Map's projection type 

// Iterate through the individual lines of our multi-line geometry 
for(int lineIndex = 0; lineIndex < geometry.getNumGeometries(); lineIndex++){ 
    Geometry lineGeometry = geometry.getGeometryN(lineIndex); 
    ArrayList<MapPos> linePositions = new ArrayList<MapPos>(lineGeometry.getCoordinates().length); 

    // Iterate through this line's coordinates 
    for(Coordinate coordinate : lineGeometry.getCoordinates()){ 
     // My server returns coordinates in WGS84/EPSG:4326 projection while the map 
     // uses EPSG3857, so it is necessary to convert before adding to the 
     // array list. 
     MapPos linePosition = new MapPos(projection.fromWgs84(coordinate.x, coordinate.y)); 
     linePositions.add(linePosition); 
    } 

    // Finally, add the line data to the vector layer 
    Line line = new Line(linePositions, new DefaultLabel("some label"), lineStyle), null); 
    geomLayer.add(line); 
} 

lineStyleSetgeomLayer先前在活動的onCreate中創建,可以研究heregeomLayer很簡單;這裏是我的lineStyleSet

備註線型,它以前創建並保存爲一個實例變量,就像這樣:

Bitmap lineMarker = UnscaledBitmapLoader.decodeResource(getResources(), R.drawable.line); 

this.lineStyleSet = new StyleSet<LineStyle>(); 
LineStyle lineStyle = LineStyle.builder().setLineJoinMode(LineStyle.NO_LINEJOIN).setWidth(0.2f).setColor(Color.RED).setBitmap(lineMarker).build(); 
lineStyleSet.setZoomStyle(minZoom, lineStyle); 
1

下面是我寫的轉換簡單(無孔的功能等),在給定的WKT成LatLang數組多邊形:

public static LatLng[] GetPolygonPoints(String poligonWkt){ 
    ArrayList<LatLng> points = new ArrayList<LatLng>(); 
    Pattern p = Pattern.compile("(\\d*\\.\\d+)\\s(\\d*\\.\\d+)"); 
    Matcher m = p.matcher(poligonWkt); 
    String point; 

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

然後可以使用所述陣列來添加在地圖上的多邊形:

LatLng[] points = GeographyHelper.GetPolygonPoints(shapeWkt); 

Polygon p = mMap.addPolygon(
    new PolygonOptions() 
     .add(points) 
     .strokeWidth(4) 
     .strokeColor(Color.RED)); 
1

只是爲了擴大對保羅的答案,因爲他的回答讓我這麼多...

private void setUpMap(SomeObjectWithLocationAsWKT r) { 

    List<LatLng> points = new ArrayList<LatLng>(); 
    WKTReader wktReader = new WKTReader(); 
    LineString line = null; 
    Coordinate lineCentroid = null; 
    Coordinate[] lineCoordinates = null; 

    // if our object's WKT geometry is not null - map it 
    if (r.geowkt != null) { 

     // use the JTS (Java Topology Suite) WKTReader to read that WKT! 
     try { 
      line = (LineString) wktReader.read(r.geowkt); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // using (JTS) again to getCoordinates of the linestring 
     lineCoordinates = line.getCoordinates(); 

     // Iterate through the line's coordinates & assign to List<LatLng> points 
     for(Coordinate coordinate : lineCoordinates){ 
      points.add(new LatLng(coordinate.x, coordinate.y)); 
     } 

     // add Polyline to Google Map 
     Polyline p = mMap.addPolyline(
     new PolylineOptions() 
     .addAll(points) 
     .width(4) 
     .color(Color.RED)); 
    } 
} 

// an example of zooming to the centroid of the WKT geometry 
// again, using (JTS - Java Topology Suite) 
private void handleNewLocation(Location location) { 
    LatLng latLng = null; 
    WKTReader wktReader = new WKTReader(); 
    LineString line = null; 
    Coordinate lineCentroid = null; 

    // if our object's WKT geometry is not null - zoom to it 
    if (r.geowkt != null) { 
     try { 
      line = (LineString) wktReader.read(r.geowkt); 
      lineCentroid = line.getCentroid().getCoordinate(); 
      latLng = new LatLng(lineCentroid.x, lineCentroid.y); 
     } catch (ParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } else { 
     // just default to whatever location was passed in 
     Log.d(TAG, location.toString()); 
     double currentLatitude = location.getLatitude(); 
     double currentLongitude = location.getLongitude(); 
     latLng = new LatLng(currentLatitude, currentLongitude); 
    } 
    CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(latLng, 19); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
    mMap.animateCamera(yourLocation); 
} 
1

一個WKT(知名文本)文件描述了ISO 19107幾何。就我個人而言,我儘量避免重新發明輪子,或者在自己寫的解析器中「亂用」它們(你永遠不知道,你的函數是否僅僅因爲它曾經工作過而覆蓋所有情況)。

所以這裏的另一個很好看的開源API,結合實例,教程:

http://docs.geotools.org/

而這裏WKTParser類

http://docs.geotools.org/stable/javadocs/org/geotools/geometry/text/WKTParser.html

Android的工作室: 只需添加這爲您的應用build.gradle文件:

dependencies { 
    /* your other dependencies */ 
    compile 'org.opengis:geoapi:3.0.0' 
}