如何在Android中创建KML文件?

时间:2011-01-07 17:28:24

标签: android google-maps kml

目前,我的应用程序从服务器接收KML文件并显示地图中的所有地标。但是有太多的数据来回传递,所以现在我想从服务器只接收一个短字符串(lat,lang为几点)。

是否可以在Android设备上创建KML文件(任何API用于此?

4 个答案:

答案 0 :(得分:2)

目前似乎没有任何KML库能够很好地与Android配合使用。我一直在使用Simple XML库来解析KML,并且运行良好。

但是,由于您已经以简化格式接收数据,因此您最好直接自己使用Google Maps API。这将节省构建KML,将其发送给Google,然后返回渲染地图的工作量。这里的教程将帮助您入门:http://developer.android.com/resources/tutorials/views/hello-mapview.html

答案 1 :(得分:1)

您可以使用libkml,但它是本地库,您需要NDK或gekmllib 这是一个Java库,您可以尝试将其包含在Android项目中。

答案 2 :(得分:1)

public class CreateKmlFileActivity extends FragmentActivity {

    private GoogleMap googleMap;
    private SupportMapFragment supportMapFragment;
    private ArrayList<LatLng> latLngArrayList = new ArrayList<>();
    private ArrayList<LatLng> latLngArrayListLine = new ArrayList<>();
    private Button button, buttonLine;
    private XmlSerializer xmlSerializer;
    private boolean flag = false;
    private Handler handler;
    private Runnable runnable;
    GPSTracker gpsTracker;
    private double lat;
    private double lng;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_task3);
        init();
        listener();
    }

    private void init() {
        button = (Button) findViewById(R.id.btn_find);
        buttonLine = (Button) findViewById(R.id.btn_line);
        supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        googleMap = supportMapFragment.getMap();
        gpsTracker = new GPSTracker(this);
        lat = gpsTracker.latitude;
        lng = gpsTracker.longitude;
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                gpsTracker.getLocation();
                if (lat != gpsTracker.latitude || lng != gpsTracker.longitude) {
                    latLngArrayListLine.add(new LatLng(gpsTracker.latitude, gpsTracker.longitude));
                    lat = gpsTracker.latitude;
                    lng = gpsTracker.longitude;
                }
                handler.postDelayed(runnable, 1000 * 60);
            }
        };
        if (!gpsTracker.getIsGPSTrackingEnabled()) {
            gpsTracker.showSettingsAlert();
        }
        //gpsTracker.latitude
    }

    private void listener() {
        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                if (!flag)
                    latLngArrayList.add(latLng);
                else
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
            }
        });

        buttonLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.removeCallbacks(runnable);
                flag = false;
                try {
                    FileOutputStream fileOutputStream = openFileOutput("testLine.kml", Context.MODE_PRIVATE);
                    xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
                    xmlSerializer.setOutput(fileOutputStream, "UTF-8");
                    xmlSerializer.startDocument(null, null);
                    xmlSerializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
                    xmlSerializer.startTag(null, "kml");
                    xmlSerializer.startTag(null, "Document");
                    xmlSerializer.startTag(null, "name");
                    xmlSerializer.text("kmlFile");
                    xmlSerializer.endTag(null, "name");
                    xmlSerializer.startTag(null, "Style");
                    xmlSerializer.attribute(null, "id", "transGreenPoly");
                    xmlSerializer.startTag(null, "LineStyle");
                    xmlSerializer.startTag(null, "width");
                    xmlSerializer.text("1");
                    xmlSerializer.endTag(null, "width");
                    xmlSerializer.startTag(null, "color");
                    xmlSerializer.text("7dff0000");
                    xmlSerializer.endTag(null, "color");
                    xmlSerializer.startTag(null, "colorMode");
                    xmlSerializer.text("random");
                    xmlSerializer.endTag(null, "colorMode");
                    xmlSerializer.endTag(null, "LineStyle");
                    xmlSerializer.endTag(null, "Style");
                    xmlSerializer.startTag(null, "Folder");
                    xmlSerializer.startTag(null, "name");
                    xmlSerializer.text("Google Campus");
                    xmlSerializer.endTag(null, "name");
                    xmlSerializer.startTag(null, "visibility");
                    xmlSerializer.text("1");
                    xmlSerializer.endTag(null, "visibility");
                    xmlSerializer.startTag(null, "description");
                    xmlSerializer.text("Your Data");
                    xmlSerializer.endTag(null, "description");
                    xmlSerializer.startTag(null, "Placemark");
                    xmlSerializer.startTag(null, "name");
                    xmlSerializer.text("Data");
                    xmlSerializer.endTag(null, "name");
                    xmlSerializer.startTag(null, "visibility");
                    xmlSerializer.text("1");
                    xmlSerializer.endTag(null, "visibility");
                    xmlSerializer.startTag(null, "styleUrl");
                    xmlSerializer.text("#transRedPoly");
                    xmlSerializer.endTag(null, "styleUrl");
                    xmlSerializer.startTag(null, "LineString");
                    xmlSerializer.startTag(null, "extrude");
                    xmlSerializer.text("1");
                    xmlSerializer.endTag(null, "extrude");
                    xmlSerializer.startTag(null, "altitudeMode");
                    xmlSerializer.text("relativeToGround");
                    xmlSerializer.endTag(null, "altitudeMode");
                    //xmlSerializer.startTag(null, "outerBoundaryIs");
                    // xmlSerializer.startTag(null, "LinearRing");
                    xmlSerializer.startTag(null, "coordinates");
                    for (int i = 0; i < latLngArrayListLine.size(); i++) {
                        if (isPointInPolygon(latLngArrayListLine.get(i), latLngArrayList))
                            xmlSerializer.text(latLngArrayListLine.get(i).longitude + "," + latLngArrayListLine.get(i).latitude + ",17 \n");
                    }
                    xmlSerializer.endTag(null, "coordinates");
                    // xmlSerializer.endTag(null, "LinearRing");
                    //xmlSerializer.endTag(null, "outerBoundaryIs");
                    xmlSerializer.endTag(null, "LineString");
                    xmlSerializer.endTag(null, "Placemark");
                    xmlSerializer.endTag(null, "Folder");
                    xmlSerializer.endTag(null, "Document");
                    xmlSerializer.endTag(null, "kml");
                    xmlSerializer.endDocument();
                    xmlSerializer.flush();
                    fileOutputStream.close();

                } catch (IOException | XmlPullParserException e) {
                    e.printStackTrace();
                }
            }
        });



    }


}

答案 3 :(得分:0)

我知道为时已晚,但OSMBonusPack可用于创建kml文件。