Fix: can not load KML file with ionic
MODIFY
src/android/plugin/google/maps/PluginKmlOverlay.java
src/android/plugin/google/maps/PluginKmlOverlay.java
package plugin.google.maps;
import android.os.Bundle;
+import android.util.Log;
import org.apache.cordova.CallbackContext;
import org.json.JSONArray;
@@ -50,6 +51,10 @@ public class PluginKmlOverlay extends MyPlugin implements MyPluginInterface {
return;
}
+ cordova.getActivity().runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+
String urlStr = null;
try {
@@ -62,6 +67,7 @@ public class PluginKmlOverlay extends MyPlugin implements MyPluginInterface {
return;
}
+ String currentPageUrl = webView.getUrl();
if (!urlStr.contains("://") &&
!urlStr.startsWith("/") &&
!urlStr.startsWith("www/") &&
@@ -72,17 +78,30 @@ public class PluginKmlOverlay extends MyPlugin implements MyPluginInterface {
}
if (urlStr.startsWith("./") || urlStr.startsWith("../")) {
urlStr = urlStr.replace("././", "./");
- String currentPage = CURRENT_PAGE_URL;
- currentPage = currentPage.replaceAll("[^\\/]*$", "");
- urlStr = currentPage + "/" + urlStr;
+ currentPageUrl = currentPageUrl.replaceAll("[^\\/]*$", "");
+ urlStr = currentPageUrl + "/" + urlStr;
}
if (urlStr.startsWith("cdvfile://")) {
urlStr = PluginUtil.getAbsolutePathFromCDVFilePath(webView.getResourceApi(), urlStr);
}
- Bundle result = loadKml(urlStr);
+ // Avoid WebViewLocalServer (because can not make a connection for some reason)
+ if (urlStr.contains("http://localhost") || urlStr.contains("http://127.0.0.1")) {
+ urlStr = urlStr.replaceAll("^http://[^\\/]+\\//", "file:///android_asset/www/");
+ }
+
+
+ final String finalUrl = urlStr;
+ executorService.submit(new Runnable() {
+ @Override
+ public void run() {
+ Bundle result = loadKml(finalUrl);
callbackContext.success(PluginUtil.Bundle2Json(result));
}
+ });
+ }
+ });
+ }
private Bundle loadKml(String urlStr) {
@@ -307,8 +326,8 @@ public class PluginKmlOverlay extends MyPlugin implements MyPluginInterface {
InputStream inputStream;
try {
- if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
//Log.d("PluginKmlOverlay", "---> url = " + urlStr);
+ if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
URL url = new URL(urlStr);
boolean redirect = true;
HttpURLConnection http = null;

Fix: can not load KML file with ionic