How can I send a kml file to Google Earth, like MyTracks (open source) do?
Asked Answered
W

4

6

I don't know if you have seen the amazing Mytrack update, but it allow to send a kml file to Google Earth app and display it inside the Google app (if installed, of course).

enter image description here

The source code is there: http://code.google.com/p/mytracks/source/browse/

but I cannot find the way to achieve such a thing.

I think I found something here: http://code.google.com/r/jshih-mytracks3/source/browse/MyTracks/src/com/google/android/apps/mytracks/io/file/SaveActivity.java?spec=svn5178eb75934b7f0c4c23ec26b7d79a0787de18b8&r=5178eb75934b7f0c4c23ec26b7d79a0787de18b8

else if (playTrack) {
        Intent intent = new Intent()
            .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
            .putExtra(GOOGLE_EARTH_TOUR_FEATURE_ID, KmlTrackWriter.TOUR_FEATURE_ID)
            .setClassName(GOOGLE_EARTH_PACKAGE, GOOGLE_EARTH_CLASS)
            .setDataAndType(Uri.fromFile(new File(savedPath)), GOOGLE_EARTH_KML_MIME_TYPE);
        startActivity(intent);

The hardcoded way gives this code:

    Intent intent = new Intent()
            .addFlags(
                    Intent.FLAG_ACTIVITY_CLEAR_TOP
                            | Intent.FLAG_ACTIVITY_NEW_TASK)
            .putExtra("com.google.earth.EXTRA.tour_feature_id","tour")
            .setClassName("com.google.earth", "com.google.earth.EarthActivity")
            .setDataAndType(Uri.fromFile(new File("/sdcard/test.kml")),
                    "application/vnd.google-earth.kml+xml");
    startActivity(intent);

But the above code simply displays the path with the same result than this code:

 Intent mapIntent = new Intent(Intent.ACTION_VIEW); 
    Uri uri1 = Uri.parse("file:///sdcard/test.kml"); 
    mapIntent.setData(uri1); 
    startActivity(Intent.createChooser(mapIntent, "Sample")); 

My objective is to get the same result, with a "play" button.

Wandie answered 14/7, 2012 at 12:15 Comment(0)
A
6

You need to specify the URI to your KML file AND the KML MIME type, as follows.

File file = new File(Environment.getExternalStorageDirectory(), "sample_tour.kml");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.google-earth.kml+xml");
intent.putExtra("com.google.earth.EXTRA.tour_feature_id", "my_track");
startActivity(intent);

This is presently undocumented, but we're looking to fix this.

Be sure to use Intent::setDataAndType and not Intent::setData and Intent::setType separately (they each override the other).

"my_track" is a reference to your placemark id. The intent extra automatically starts the tour.

<Placemark id="my_track">
 <gx:Track>
  ...
 </gx:Track>
</Placemark>
Alibi answered 14/8, 2012 at 0:3 Comment(0)
L
3

Is it possible to use a link instead of a kml from disk? Something like this:

intent.setDataAndType(Uri.parse("http://sites.cyclingthealps.com/other/downloads/doc.kml"), "application/vnd.google-earth.kml+xml");

Thanks

Lout answered 5/12, 2012 at 20:46 Comment(0)
R
2

If you are targeting Android N, you are not allowed to send a file:// intent anymore. Use the instructions at android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() , and add the Intent.FLAG_GRANT_READ_URI_PERMISSION flag.

Rationalism answered 19/3, 2017 at 10:9 Comment(1)
Was there also a change from Android 5 to 6? My app can open a KML in Google Earth in Android 5. In Android 6, the Google Earth opens, but it apparently did not receive the KML file?Parvenu
F
0
File KML = new File("/sdcard/doc.kml");
Intent i = getPackageManager().getLaunchIntentForPackage("com.google.earth");
i.setDataAndType(Uri.fromFile(KML), "xml");
startActivity(i);

source: http://enladodelbien.blogspot.com/2015/06/kmlkmz-to-google-earth.html

Fortissimo answered 3/7, 2015 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.