KML on Google Maps in android application

2020-08-01 03:58发布

问题:

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

回答1:

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.



回答2:

I try to create a variable of the class KmlLayer but don´t recognize it.

Autoimport don´t import kmllayer and show error.

There are my imports:

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

This is my code:

 KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.salamanca_ceas, getApplicationContext());
    kmlLayer.addLayerToMap();
    moveCameraToKml(kmlLayer);

what is wrong?