如何在Android中处理WKT数据?
我有以下格式的数据:如何在Android中处理WKT数据?,android,google-maps,wkt,Android,Google Maps,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.0136031
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());
ArrayList坐标=新建ArrayList
add(tvcoor.getText().toString());
这会产生一个错误:当我运行应用程序时,它会强制停止。
我怎样才能在地图上画呢?试试这个
String str;
ArrayList<String> coordinates = new ArrayList<String>();
第二,拆下支架
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 + "-");
}
}
for(int i=0;i
对于其他具有相同场景的人,我发现了一个名为的开源库,它能够在Java中解析WKT字符串。我的应用程序中的一个基本示例如下所示:
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
}
请注意,上面是一个非常通用的例子,它遵循OP的代码
最终,这可能会使其他人不必使用自己的WKT解析器
Nutiteq特定代码:
在我的例子中,我需要绘制一个多行字符串作为一个向量层。我知道OP询问的是google map android API,但是如果任何读者也在使用Nutiteq(或者如果此算法与其他map 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);
}
下面是我编写的一个函数,用于将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()]);
}
我想进一步说明保罗的回答,因为他的回答对我帮助很大
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);
}
private void setUpMap(SomeObjectWithLocationAsWKT r){
列表点=新的ArrayList();
WKTReader WKTReader=新WKTReader();
LineString line=null;
坐标线形心=空;
坐标[]lineCoordinates=null;
//如果对象的WKT几何体不是空的,请映射它
如果(r.geowkt!=null){
//使用JTS(Java拓扑套件)WKTReader读取该WKT!
试一试{
line=(LineString)wktReader.read(r.geowkt);
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//再次使用(JTS)获取线串的坐标
lineCoordinates=line.getCoordinates();
//遍历直线坐标并指定给列表点
用于(坐标:线坐标){
添加点(新板条(坐标x,坐标y));
}
//将多段线添加到Google地图
多段线p=mMap.addPolyline(
新的多段线选项()
.addAll(点数)
.宽度(4)
.颜色(颜色.红色));
}
}
//缩放到WKT几何体质心的示例
//同样,使用(JTS-Java拓扑套件)
专用无效句柄ewlocation(位置){
LatLng LatLng=null;
WKTReader WKTReader=新WKTReader();
LineString line=null;
坐标线形心=空;
//如果对象的WKT几何体不是空的,则缩放到它
如果(r.geowkt!=null){
试一试{
line=(LineString)wktReader.read(r.geowkt);
lineCentroid=line.getCentroid().getCoordinate();
latLng=新的latLng(线质心.x,线质心.y);
}捕获(解析异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}否则{
//只是默认为传入的任何位置
Log.d(标记,location.toString());
double currentLatitude=location.getLatitude();
double currentLength=location.getLength();
latLng=新latLng(当前纬度、当前经度);
}
CameraUpdate yourLocation=CameraUpdateFactory.newLatLngZoom(latLng,19);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(您的位置);
}
WKT(众所周知的文本)文件描述了ISO 19107几何图形。就我个人而言,我尽量避免重新发明轮子,或在自行编写的解析器中“弄乱”它们(你永远不知道,你的函数是否只因为工作了一次就涵盖了所有情况)
下面是另一个漂亮的开源API,包括示例和教程:
这里是WKTParser课程
安卓工作室:
只需将其添加到应用程序build.gradle
文件:
dependencies {
/* your other dependencies */
compile 'org.opengis:geoapi:3.0.0'
}
Gilles我添加了if语句,因为textView还收到了另一个数据。但是我只想将坐标数据存储到数组列表中感谢我至少解决了我的错误告诉我这段代码是否正确,可以从textviewif((tvTextIn.equals(new String(“POINT”)))| |(tvTextIn.equals(new String(“multiline”)){coordinates.add(tvcoor.getText().toString());}当我运行这段代码时,ArrayList coordinates=new ArrayList coordinates.add(tvcoor.getText().toString());它不在数组列表中存储值。所有值都为空。它创建了一个大小,但所有值都为空。您是否获得textview值。请发布您的完整代码。感谢rajesh的帮助,但我无法理解最后一步,以及当我在mapactivity中发送此coordiantes arraylist,然后从mapactivity发送到overlay类并在=getIntent()中绘制pointIntent时的步骤;loc=in.getStringArrayListExtra(“stringname”);mapOverlays=mapView.getOverlays();proj=mapView.getProjection();覆盖层=新的MyOverlay(位置);然后画一个像这样的点公共MyOverlay(列表位置){//TODO自动生成的构造函数存根GeoPoint mp=new GeoPoint((int)(Integer.parseInt(locations.get(0))*1E6),(int)(Integer.parseInt(locations.get(1))*1E6));然后应用程序意外地停止它
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));
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);
}
dependencies {
/* your other dependencies */
compile 'org.opengis:geoapi:3.0.0'
}