How to show each points name as a label in the offline map?

238
2
11-21-2018 11:10 PM
AlisherMirzoev
New Contributor

Hello, I'm developing offline map for iOS using TPK package as a basemap and some local shape files for feature layers. Everything is working fine except labeling. I have a huge data more than thousand points in the map which is loading from table in shape file, I want to show each POI name as a text/label instead of dot or it could be placed next to dot in the map. I've tried to see documentation and I found only Label expression which is for online use only, but no luck for my case. Is there any other solution? I also tried clusters but it seems for symbols and online data only. May be I cannot getting there how to implement it in offline environment ? Thanks in advance.

0 Kudos
2 Replies
RyanOlson1
Esri Contributor

You can add labels to shapefile data. We do not have a comprehensive api for creating the label definition through properties, however one can be created with json like so:

            let map = AGSMap(basemapType: .lightGrayCanvas, latitude: 0, longitude: 0, levelOfDetail: 0)
           
            let shp1 = AGSShapefileFeatureTable(fileURL: URL.TestDataURL("Shapefile/FromArcMap/CapitalsFromAM.shp"))
            let fl1 = AGSFeatureLayer(featureTable: shp1)
       
            map.operationalLayers.add(fl1)
           
            let jsonStr = """
                               {"labelPlacement":"esriServerPointLabelPlacementAboveRight","where":null,"labelExpression":"[CITY_NAME]","useCodedValues":true,"symbol":{"type":"esriTS","color":[255,0,0,255],"backgroundColor":null,"borderLineColor":null,"borderLineSize":null,"verticalAlignment":"bottom","horizontalAlignment":"center","rightToLeft":false,"angle":0,"xoffset":0,"yoffset":0,"kerning":true,"haloColor":[255,255,255,255],"haloSize":2,"font":{"family":"Arial","size":10,"style":"normal","weight":"normal","decoration":"none"}},"minScale":0,"maxScale":0}
                               """
           
            guard let jsonData = jsonStr.data(using: .utf8) else{
                return
            }
           
            let jsonObject = try JSONSerialization.jsonObject(with: jsonData, options: [])
           
            let labelDefinition = try AGSLabelDefinition.fromJSON(jsonObject)
           
            fl1.labelsEnabled = true
            fl1.labelDefinitions.add(labelDefinition)
           
            mapView.map = map‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

That code will give label the `CITY_NAME` field for each point in the shapefile. It will be a red (255,0,0,255) text symbol with a white halo.

0 Kudos
AlisherMirzoev
New Contributor

Thank you very much, it's a great approach!

But I see Arabic fonts doesn't support at all, only English for now and I got error "Errors thrown from here are not handled" for "try". Then it worked fine when I put it inside do { } catch handler. 

0 Kudos