wkt android,How to handle WKT data in Android?

本文介绍了一个名为JTSTopologySuite的开源库,用于在Java中解析Well-Known Text (WKT)字符串。作者分享了如何在Nutiteq地图上以向量层显示多线段,包括坐标转换和线样式设置的详细步骤,旨在帮助开发者避免从头开始编写WKT解析器。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值