views:

72

answers:

3

Hello,

could some one please tell me if there is a way to load kml file into the google map in android.

thanks

A: 

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...

WarrenFaith
+1  A: 

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

http://stackoverflow.com/questions/2470948/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.

Scoobler
@Scoobler...thanks for that. So how do I over lay this kml file on the google map?
raqz
@raqz what exactly do you mean? Do you have a kml file which you want to overlay on the map - or do you have a set of co-ordinates and you are looking for a way to show them on a map i.e. you are creating a kml file dynamically?
Scoobler
@Scoobler... I have a kml file whose data is constant. I need to plot those geometries in the kml on the map ie; overlay that on the google map.Also, I have another kml file which is generated dynamically on the server and i gotta display the same on the phone. so the structure would be, first google map, next constant kml file and then finally on top of both these..its the dynamically generated kml. is there a way to do. really appreciate your help...thanks
raqz
@raqz I would say there are a couple of ways you could look to do that - if you want to use the map app to do the work for you. It seems like you have a decision to make - you say the dynamic file is made on your server. You could transmit a static file from the phone to the server have your server include its data and place it in one dynamic file and give you a link, this will mean the least processing on the phone (easy). Alternatively you can use a map activity, create your own overlay with all the points from a KML and the extra dynamic data. The first is easy but relies on a data signal.
Scoobler
@Scoobler..thank you for your ideas...but could you please elaborate how can i achieve that phenomenon of making the map app do the work for me. i have the kml file with all the info. how do i make the map app retrieve both google map and the kml as an overlay as one whole component. like u said, i processed the enntire kml on the phone, but the phone crashes as it runs out of memory... please help...
raqz
@raqz see the update for some extra details.
Scoobler
+1  A: 

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

http://stackoverflow.com/questions/3109158/how-to-draw-a-path-on-a-map-using-kml-file/3109723#3109723

Mathias Lin
@Mathias...thank you so much! but if i may ask you. is there a way to access the kml from the phone sd card. the kml i am using will never change. so its no point receiving it from the server every time turn the app on. thanks again
raqz
sure, you can put it in a text file and read it via the regular java.io.file methods
Mathias Lin
so you mean to say.. I need to copy the kml contents into a text file and parse it using io method..(string tokenizer and stuff like that)?is it not possible to store the kml in the phone and use sax parser to parse it?
raqz
hm, not sure what you mean. reading the file is not directly related with parsing it. of course you can use the sax parser, but you need to get the content to parse from somewhere in order to pass it to the sax parser. or if your sax parser allow to just pass a filename as parameter, of course then you don't need to load it yourself manually. I think any input source will work, like xr.parse(new InputSource(aUrl.openStream()));
Mathias Lin
i stored the kml file into the phone, parsed it using SAx parser and then overlayed the same on the google map.. it worked out fine..but since the number of LineStrings was large, the phone crashed as it ran out of memory.. is there a way to retrieve google map + overlayed kml file (as one object) to the phone? in that way..the phone doesnt not have to do all the computation...
raqz
that must be quite a large route then. how large is your kml file on the phone? or how many line strings? it must mean that you're actually using all 24 mb (Nexus One, Desire, etc.) or even 48 mb (SGS) of memory depending on your phone. Sure it's the kml data that's causing the memory issue?
Mathias Lin