I am using the Ionic framework for about one day and I absolutely love it already. I started building an App that uses the leaflet framework for it’s maps; but I’ve hit a problem.
The leaflet map is working in the browser (ionic serve) on iOS, Android and OSX; but when I build the app and run it as a native app on iOS it doesn’t work anymore. The map simply becomes gray. I have a screenshot here:
It got even more strange now, since the markers are being added at the right location; and you can zoom in and out, which means that all the logic is working. Only the tiles seem to cause problems.
Notice the protocol isn’t defined in src tag. The browser assumes http://, but within the iOS simulator at least file:// is assumed. Anybody else has this problem? And found a fix?
I hope this helps, it did work for me! Note that you need the Angular-Leaflet-plugin for this
I think because I now use a specific “url = http://…”, leaflet knows that I am talking about external links and thus doesn’t change them to file://
Well, your solution didn’t work for me, but based on your solution i think i found the way to deal with it.
Here is how i initiate my map inside the controller based on leaflet docs.
I think the solution was not actually found here, but a hack was created. “Whatever works” is sometimes okay, but when you don’t understand why something doesn’t work it can give you headaches later.
From what I’ve found, the problem of why the tiles show in a browser but not when deployed to a device is caused by the Content-Security-Policy (CSP) of the WebView used in the native applications. A plugin has been developed to address this, but I have not yet figured out how to configure it 100%.
it seems to work both in the browser and on my Samsung Galaxy S4 (Android 5.0). The hack described above probably works because it meets the default CSP requirements. Looking at the documentation for the plugin it is simple to add specific domains that should be trusted (the tile service), but it is not very easy to figure out what (sub)domains to include.
If you install the plugin, without adding anything, it seems to run… for some reason, but you will get a bunch of warning in your console saying “No Content-Security-Policy meta tag found. Please add one when using the cordova-plugin-whitelist plugin.” With these services like Google maps or Leaflet, it isn’t straightforward figuring out what domains to include, because they seem to call several different sub-domains. I still have yet to try out wild-cards to make it work how it is supposed to.
*.openstreetmap.org -> allow access to openstreetmap (of course )
For test purpose (all access allowed, bad to use as production mode): <meta http-equiv="Content-Security-Policy" content="default-src *; style-src * 'unsafe-inline'; media-src *; img-src *; script-src * 'unsafe-inline' 'unsafe-eval'">
Don’t forget to add permission to your config.xml:
HI dionesandrade, i tried every step. When i run ionic app in device, it give error data: gap: https://ssl.gstatic.com in Chrome inspect console.
Kindly help me out, i am stuck in this for 2 days
Regards
it happened to me these 2 days, so it was a stupid bug , that the cold didn’t load the map function , please double check ,debugging use some alert to find out that your map is already loaded
and second thing is , if you set load map function in side the other promise function , like get location , so be careful about that