当前位置: 首页 > 工具软件 > WKT Parser > 使用案例 >

wkt android,google maps - How to handle WKT data in Android? - Stack Overflow

刘阳荣
2023-12-01

For anyone else who has this same scenario, I found an open source library called JTS Topology Suite, which has the capability to parse WKT strings in Java. A basic example from my application looks like this:

WKTReader wktReader = new WKTReader();

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

You can then iterate through the individual lines like so:

for(int lineIndex = 0; lineIndex < geometry.getNumGeometries(); lineIndex++){

Geometry lineGeometry = geometry.getGeometryN(lineIndex);

//... other stuff

}

If necessary, you can obtain the individual coordinates of each line like so:

Coordinate[] lineCoordinates = lineGeometry.getCoordinates();

Note that the above is a very general example which follows the OP's code.

Ultimately, this might save others from having to roll their own WKT parser.

Nutiteq-Specific Code:

In my case, I needed to draw a multiline string as a vector layer on a Nutiteq map. I realize the OP was asking about the google map android API, however in case any reader is also using Nutiteq (or if this algorithm is relevant for other map APIs), this is the nutiteq specific code:

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 linePositions = new ArrayList(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);

}

Note that the lineStyleSet and geomLayer are created previously in the activity's onCreate and can be researched here. The geomLayer is simple; here is my lineStyleSet:

Note about lineStyle, it was created previously and is saved as an instance variable, like so:

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

this.lineStyleSet = new StyleSet();

LineStyle lineStyle = LineStyle.builder().setLineJoinMode(LineStyle.NO_LINEJOIN).setWidth(0.2f).setColor(Color.RED).setBitmap(lineMarker).build();

lineStyleSet.setZoomStyle(minZoom, lineStyle);

 类似资料: