开发者

Android: how to load KML

could some one please tell me if there is a wa开发者_Python百科y to load kml file into the google map in android.

thanks


Please find a code sample here that gets the kml data from google and draws it onto the map:

How to draw a path on a map using kml file?


Several months ago I was looking for a similar function, but couldn't find a way to load a file from the SDCard - or even using a content provider. I haven't looked since.

However one method I spotted, which proved useful as I was already communicating with a webserver

getting maps to accept a dynamically generated KML file?

This allowed me to dispatch the KML from my app to the server (communicating was already in place), the server stored the KML, assigned a random string, and returned it to the app, the app then passed a link to the server in the style:

final Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=http://website.com/kml_gen.php?id=1kj312"));
startActivity(myIntent);

The server returned a KML list for the map app to use.

An update after comments below:

final Intent myIntent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=http://code.google.com/apis/kml/documentation/KML_Samples.kml"));
startActivity(myIntent);

Try running the above code - you will see when you run it, the google map app is opened, and it will request the kml file, in this case the samples file from google. This is a map with an overlay, but it is now the google map application and no longer being run inside your application.

For this solution you would need to store your KML file on a web server somewhere - this has a slight problem in that the user needs a data connection (which the map would need anyway - but if the kml file is large and the connection slow there maybe a delay).

You mentioned you had a server producing some dynamic data and also some static stuff. If the static stuff is the same for everyone and every time a server produces some data I would be tempted to have the web server produce one well formatted KML document including both sets of data - the google maps application, as far as I know will only load one KML file from a webserver.

The other solution, which is one I have used has been posted above - if you have the static data on the phone (I would go with a DB with the data), create an overlay drawing the items on, and also request the dynamic results from the server, add those to the custom overlay. This option will mean you will have to code any extra function you want - i.e. searching the map for a location, satelite view..... Be sure to perform the operation in a seperate thread and not the UI thread, specially when building large overlays or you are likely to get a Force Close / Wait dialog.


I used GPX files on a map (Android 1.5) but there wasn't anything to load KML or GPX. If it wasn't added in the newer versions of Android, I guess you have to read the file on your own. Thats what I did 10 month ago...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜