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

て烟熏妆下的殇ゞ 提交于 2019-12-02 19:49:11

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>

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

rockgecko

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!