Android Google Maps/Earth opening Local KML/KMZ file and play the tour functionality

人盡茶涼 提交于 2021-02-19 07:40:48

问题


Is there a way to open a local KML/KMZ file in Google Maps/Earth app on Android? Tried the below approach but didn't work.

    Intent mapIntent = new Intent(Intent.ACTION_VIEW);
    Uri uri1 = Uri
            .parse("geo:0,0?q=file:///mnt/sdcard/doc.kml");
    mapIntent.setData(uri1);
    startActivity(Intent.createChooser(mapIntent, "Sample"));

If not and if we can specify only links that are hosted on the web then can we specify a link to the Google Drive file to show directly on Google Maps/Earth?

    Intent mapIntent = new Intent(Intent.ACTION_VIEW);
    Uri uri1 = Uri
            .parse("geo:0,0?q=https://drive.google.com/open?id=0B8n3LAJCTg-8eml4TTBoZDlRd00&authuser=0");
    mapIntent.setData(uri1);
    startActivity(Intent.createChooser(mapIntent, "Sample"));

And finally what happened to the play tour functionality in Google Earth? It used to work but with the latest updates its broken and no longer working.

    File file = new File(playFileNameKml);
    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", "tour");
    startActivity(intent);

回答1:


As I know, in Maps its impossible since the URI must be online address that google servers can access. Regarding Earth, that was working for me:

     path = Environment.getExternalStorageDirectory() + "/file.kml";
     packageName = "com.google.earth";
     Intent earthIntent = new Intent(android.content.Intent.ACTION_VIEW);
     earthIntent.setDataAndType(Uri.parse("file:/"+ path), "application/vnd.google-earth.kml+xml");
     earthIntent.setClassName(packageName, "com.google.earth.EarthActivity");
     targetedShareIntents.add(earthIntent);



回答2:


I try this peace of code and it works great, except that you have to change permission to your kml file

File file = new File(playFileNameKml);

file.setReadable(true, false); // for reading you can add for writing  //and/or ecuting if you need that
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", "tour");
startActivity(intent);



回答3:


This is what has just worked for me:

Uri uriFromFile = FileProvider.getUriForFile(Install_sinapseActivity.this, GenericFileProvider.class.getName(), file); try { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uriFromFile, "application/vnd.google-earth.kml+xml"); intent.putExtra("com.google.earth.EXTRA.tour_feature_id", "my_track"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); catch (ActivityNotFoundException e) { ... }



来源:https://stackoverflow.com/questions/27058193/android-google-maps-earth-opening-local-kml-kmz-file-and-play-the-tour-functiona

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