How to handle WKT data in Android?

前端 未结 5 1265
傲寒
傲寒 2020-12-18 11:22

I have data in this format:

POINT(73.0166738279393 33.6788721326803)
MULTILINESTRING((73.0131224998036 33.679001500419,73.0119635003153 33.678392400389,73.01         


        
5条回答
  •  猫巷女王i
    2020-12-18 11:38

    Just to expand on Paul's answer, since his answer helped me so much...

    private void setUpMap(SomeObjectWithLocationAsWKT r) {
    
        List points = new ArrayList();
        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 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);
    }
    

提交回复
热议问题