I'm trying to load the Traffic service using the AGSPortalItem
AGSPortalItem(url: URL(string: "http://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer")!)
AGSFeatureLayer(item: portalItem, layerID: 7)
But I got an error on the application when I try to load this layer "Invalid portal item URL."
Solved! Go to Solution.
Thank you for your question! The issue you're having is that the url points to a Map Service and the code above is attempting to load it as portal item.
You have a couple of options, the first is to use the code you have, but use the World Traffic Service portal item and use an "AGSArcGISMapImageLayer" instead of a feature layer (because the item represents a map service):
let trafficItem = AGSPortalItem(url: URL(string: "https://www.arcgis.com/home/item.html?id=ff11eb5b930b4fabba15c47feb130de4")!)!
let trafficLayer = AGSArcGISMapImageLayer(item: trafficItem)
The second is to use the url you currently have, but instantiate an "AGSArcGISMapImageLayer", like below
let trafficLayer = AGSArcGISMapImageLayer(url: URL(string:"https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer")!)
map.operationalLayers.add(trafficLayer)
Also, it is best practice to ALWAYS use "https" as opposed to simply "http" for security.
You will need to log in with a named user to see the data.
And if you haven't checked out our new Swift API, you can find it here!
Let me know if you have any more issues!
Mark
If you look at the "Terms of Use" in the item's description it says:
So you won't be able to use an API key. You do have a couple of other options. Depending on your use case, you can hard-code your user credentials in your app and have the service use those.
let portal = AGSPortal.arcGISOnline(withLoginRequired: true)
portal.credential = AGSCredential(user: <#T##String#>, password: <#T##String#>)
portal.load() { [weak self] error in
let trafficItem = AGSPortalItem(portal: portal, itemID: "ff11eb5b930b4fabba15c47feb130de4")
let trafficLayer = AGSArcGISMapImageLayer(item: trafficItem)
self?.map.operationalLayers.add(trafficLayer)
}
Or, you can follow the instructions to "Register an application". You can also find iOS SDK doc for security and authentication here.
Mark
Thank you for your question! The issue you're having is that the url points to a Map Service and the code above is attempting to load it as portal item.
You have a couple of options, the first is to use the code you have, but use the World Traffic Service portal item and use an "AGSArcGISMapImageLayer" instead of a feature layer (because the item represents a map service):
let trafficItem = AGSPortalItem(url: URL(string: "https://www.arcgis.com/home/item.html?id=ff11eb5b930b4fabba15c47feb130de4")!)!
let trafficLayer = AGSArcGISMapImageLayer(item: trafficItem)
The second is to use the url you currently have, but instantiate an "AGSArcGISMapImageLayer", like below
let trafficLayer = AGSArcGISMapImageLayer(url: URL(string:"https://traffic.arcgis.com/arcgis/rest/services/World/Traffic/MapServer")!)
map.operationalLayers.add(trafficLayer)
Also, it is best practice to ALWAYS use "https" as opposed to simply "http" for security.
You will need to log in with a named user to see the data.
And if you haven't checked out our new Swift API, you can find it here!
Let me know if you have any more issues!
Mark
Can I use this layer without showing the credentials popup maybe using my api key "AGSArcGISRuntimeEnvironment.apiKey"?
If you look at the "Terms of Use" in the item's description it says:
So you won't be able to use an API key. You do have a couple of other options. Depending on your use case, you can hard-code your user credentials in your app and have the service use those.
let portal = AGSPortal.arcGISOnline(withLoginRequired: true)
portal.credential = AGSCredential(user: <#T##String#>, password: <#T##String#>)
portal.load() { [weak self] error in
let trafficItem = AGSPortalItem(portal: portal, itemID: "ff11eb5b930b4fabba15c47feb130de4")
let trafficLayer = AGSArcGISMapImageLayer(item: trafficItem)
self?.map.operationalLayers.add(trafficLayer)
}
Or, you can follow the instructions to "Register an application". You can also find iOS SDK doc for security and authentication here.
Mark