Adding a KML layer to an Android maps application

2020-02-11 07:53发布

问题:

I have been starting out in the world of Android development recently and I am working on an application which shows a map using the Google API SDK, it is pretty much the original example but edited a little bit. It works a treat but I wish to do more with it. My question is:

How do you add a KML layer to this? I've not found many examples out there and none of them are very clear. I know how to do this with Javascript on websites but not in android.

Can anyone help?

回答1:

Google added a support to KML recently in its utility lib. You can instantiate a KMLLayer as below:

KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext());

Or if you have a stream (maybe the kml you need is on a remote server):

KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());

After this you have to add the layer to the map:

layer.addLayerToMap();

More info on Google Maps KML Importing Utility. The library is here: Google Maps Android API utility library (you need 0.4+ version)

CAUTION: At the time, it's a Beta feature. In fact it seems you can't make any modifications to polygons, changing their styles, and so on...

I hope it helped.



回答2:

If you are ready to move to OpenSteetMap maps, then you can use osmdroid and OSMBonusPack to have KML support for: reading, saving, displaying and styling.

There is a dedicated tutorial about KML.