问题
I have multiple KML files which are drawn in google earth and contain different routes. Now I'm trying to display those in my android project with Maps API V2.
Is there an existing library for importing KML files in your android project and displaying them in maps? I found some code on stack overflow ( How to draw a path on a map using kml file? ) which isn't a library.
If there's no library available I'm just going to build this from scratch.
回答1:
For now Ill just assume that theres no public library that does this for us so I'm going to use Google's code to add polylines and polygons to my map after parsing the data in my KML file. Will update this answer if a library is found.
Create Polylines and Polygons:
// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)); // Closes the polyline.
// Set the rectangle's color to red
rectOptions.color(Color.RED);
// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);
回答2:
Just an update on the KML library for Maps API V2 part of the question. There is now a beta available of Google Maps KML Importing Utility.
It is part of the Google Maps Android API Utility Library. As documented it allows loading KML files from streams
KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());
or local resources
KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext());
After you have created a KmlLayer, call addLayerToMap() to add the imported data onto the map.
layer.addLayerToMap();
来源:https://stackoverflow.com/questions/14442275/import-kml-in-maps-api-v2