如何在Android中创建KML文件?

发布于 2024-10-10 13:27:32 字数 127 浏览 4 评论 0原文

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

是否可以在 Android 设备上创建 KML 文件(有任何 API 吗?

At the moment, my app receives KML file from server and displays all the placemarks in the maps. But there is too much data going back and forth, so now i want to receive only a short string (lat,lang for a few points) from the server.

Is it possible to create KML file on an Android device (any API for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦途 2024-10-17 13:27:33

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

I know it is too late but OSMBonusPack can be used to create kml files.

段念尘 2024-10-17 13:27:32

目前似乎没有任何 KML 库可以与 Android 很好地配合。我一直在使用 Simple XML 库来解析 KML,并且效果很好。

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

There does not seem to be any KML libraries that will work well with Android right now. I have been using the Simple XML library for parsing KML, and is working out well.

However, since you are already receiving the data in a simplified format, you would be much better off using the Google Maps API directly yourself. This will save the effort of building up the KML, sending it to Google, and then getting back the rendered map. The tutorial here will help you get started: http://developer.android.com/resources/tutorials/views/hello-mapview.html

戏舞 2024-10-17 13:27:32

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

You may use libkml, but it's a native library and you would need the NDK or gekmllib
which is a Java library you can try to include in your Android project.

神回复 2024-10-17 13:27:32
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();
                }
            }
        });



    }


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



    }


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文