Select to view content in your preferred language

Fail to load Traffic Service

1101
3
Jump to solution
07-11-2023 07:46 AM
PauloKeller
New Contributor

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."

0 Kudos
2 Solutions

Accepted Solutions
MarkDostal
Esri Regular Contributor

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

 

View solution in original post

0 Kudos
MarkDostal
Esri Regular Contributor

If you look at the "Terms of Use" in the item's description it says:

Important Note: This item requires an ArcGIS Online organizational subscription or an ArcGIS Developer account and does not consume credits. To access this item, you'll need to do one of the following:
  • Sign in with an account that is a member of an organizational subscription
  • Sign in with a developer account
  • Register an application and use your application's credentials.

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

View solution in original post

0 Kudos
3 Replies
MarkDostal
Esri Regular Contributor

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

 

0 Kudos
PauloKeller
New Contributor

Can I use this layer without showing the credentials popup maybe using my api key "AGSArcGISRuntimeEnvironment.apiKey"?

0 Kudos
MarkDostal
Esri Regular Contributor

If you look at the "Terms of Use" in the item's description it says:

Important Note: This item requires an ArcGIS Online organizational subscription or an ArcGIS Developer account and does not consume credits. To access this item, you'll need to do one of the following:
  • Sign in with an account that is a member of an organizational subscription
  • Sign in with a developer account
  • Register an application and use your application's credentials.

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

0 Kudos