Search code examples
androidgoogle-mapsoverlaykml

KML on Google Maps in android application


Can anyone know how to add the KML layer on the android app as an overlay in the google map.

The KML is not a static KML file that may uploaded by the user only. And i have to upload the KML to my server then i want to show the corresponding KML layer in the google map which is displaying in the android application itself.

Have any idea ?. Please suggest some examples.

I'm new in the KML. So please...


Solution

  • To do the same...

    1) Make one folder under your resource folder( named as "raw" (Do whate ever you want to give. Please dont forget to change the name while accessing the KLM file )) eg:- res/raw

    2) Place your KML in this folder.

    3) In your activity,

    Create the variable for the layer

     KmlLayer layer;
    

    4) In your onCreate function just call the KML file. Ie create the layer.

    layer = new KmlLayer(googleMap, R.raw.landmarkkml, getApplicationContext());
    

    here there are three arguments.

    1st one google map object. 2nd one KML file path . 3rd the application context.

    5) Now we have to add the kmllayer with the google map.To do that,

     layer.addLayerToMap();
    

    Complete code

    package com.yourdomain.kmlongooglemap;
    import android.app.Activity;
    import android.content.Context;
    public class KmlActivity extends Activity {
    private GoogleMap googleMap; // Google map object.
    KmlLayer layer; //KML object.
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kmlactivitylayout); 
        try {
            layer = new KmlLayer(googleMap,R.raw.lanmark, getApplicationContext()); // creating the kml layer
            layer.addLayerToMap();// adding kml layer with the **google map**
        } catch (Exception e) {
            e.printStackTrace();
        }
      });
    
    }
    

    the you can run your application.Here you can see the KML on your GOOGLE MAP.

    Thanks.