I've been able to load background tile packages using QML similar to this:
MapView {
id: mapView
Map {
id: map
Basemap {
id: baseMap
ArcGISTiledLayer {
TileCache {
id: tileCache
path: file:///C:/tile_packages/landsat_2017.tpk
}
}
}
}
}<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>But I don't like the idea of hard-coding the tile package path into the app and would prefer to allow users to set the path in a config/settings file or local storage database. But I can't seem to figure out the JavaScript needed to dynamically create the ArcGISTiledLayer + TileCache and add it to the Basemap/Map. Can anyone share some code to replicate what I have above in QML such that the tile cache path can come from a variable?
This is what I've tried in the ApplicationWindow Component.onCompleted, but I get the error "Cannot assign QObject* to QmlArcGISItem*" on line 5 below when I try to set the item property on the ArcGISTiledLayer to the TileCache object.
<SPAN class="keyword token">var</SPAN> tilePackage <SPAN class="operator token">=</SPAN> ArcGISRuntimeEnvironment<SPAN class="punctuation token">.</SPAN><SPAN class="token function">createObject</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"TileCache"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
tilePackage<SPAN class="punctuation token">.</SPAN>path <SPAN class="operator token">=</SPAN> tilePackagePath<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// from config/settings/local storage db</SPAN>
<SPAN class="keyword token">var</SPAN> tiledLayer <SPAN class="operator token">=</SPAN> ArcGISRuntimeEnvironment<SPAN class="punctuation token">.</SPAN><SPAN class="token function">createObject</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ArcGISTiledLayer"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
tiledLayer<SPAN class="punctuation token">.</SPAN>item <SPAN class="operator token">=</SPAN> tilePackage<SPAN class="punctuation token">;</SPAN>
tilePackage<SPAN class="punctuation token">.</SPAN>loadStatusChanged<SPAN class="punctuation token">.</SPAN><SPAN class="token function">connect</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">function</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>tilePackage<SPAN class="punctuation token">.</SPAN>loadStatus <SPAN class="operator token">===</SPAN> Enums<SPAN class="punctuation token">.</SPAN>LoadStatusLoaded<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"LoadStatusLoaded"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
mapView<SPAN class="punctuation token">.</SPAN>map <SPAN class="operator token">=</SPAN> tiledLayer<SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>tilePackage<SPAN class="punctuation token">.</SPAN>loadStatusChanged <SPAN class="operator token">===</SPAN> Enums<SPAN class="punctuation token">.</SPAN>LoadStatusFailedToLoad<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN>
console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">log</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"failed to load tpk"</SPAN><SPAN class="punctuation token">,</SPAN> error<SPAN class="punctuation token">.</SPAN>message<SPAN class="punctuation token">,</SPAN> error<SPAN class="punctuation token">.</SPAN>additionalMessage<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
tilePackage<SPAN class="punctuation token">.</SPAN><SPAN class="token function">load</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Thanks!