map scalebar in ios api

3355
4
03-04-2013 04:31 PM
HumzaAkhtar
Occasional Contributor II
Hi all,

I could not find any map scale bar in the reference of iOS api? Does 10.1.1 sdk doesnot have this capability to add a scale bar unlike SDK for WPF or SDK for java script?

Thanks
0 Kudos
4 Replies
NimeshJarecha
Esri Regular Contributor
No, SDK does not provide scale bar. You'll have to write your own.

Regards,
Nimesh
0 Kudos
AkhilChoran_Balan
New Contributor

Just wondering if this is still not available. Anyone has any solution on how to do this?

0 Kudos
ShiminCai
Occasional Contributor II

Hi Akhil,

I was also looking for a scalebar solution in the forum but couldn't find any. So I did a very simple scalebar (see the attached image).

The idea is to use a fixed-width image view to show a scalebar image. Then calculate the width of the image view in map unit which is map scale dependent. In another word the width of the image view represents different map distances at different map scales.

override func viewDidLoad()

    {

        super.viewDidLoad()

       

        // Do any additional setup after loading the view, typically from a nib.

       

        // set up and load your map here

        //Register map pan and zoom notifications for scale bar

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "mapViewDidEndPanningZooming", name: AGSMapViewDidEndZoomingNotification, object: nil)

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "mapViewDidEndPanningZooming", name: AGSMapViewDidEndPanningNotification, object: nil)

      

         // the reason to register the AGSMapViewDidEndPanningNotification is to account for the device rotations.

    }

func mapViewDidEndPanningZooming()

    {

        //Scale

        var formatter = NSNumberFormatter()

        formatter.numberStyle = NSNumberFormatterStyle.DecimalStyle

        let groupingSeparator: String! = NSLocale.currentLocale().objectForKey(NSLocaleGroupingSeparator) as! String

        formatter.groupingSeparator = groupingSeparator

        formatter.groupingSize = 3

        formatter.alwaysShowsDecimalSeparator = false

        formatter.usesGroupingSeparator = true

        var scale: Double = round(self.mapView.mapScale)

        self.scaleLabel.text = "1 : " + formatter.stringFromNumber(NSNumber(double: scale))!

       

        //Scale bar

        let screenPointStart = CGPoint(x: self.scalebar.frame.origin.x, y: self.scalebar.frame.origin.y)

        let mapPointStart = self.mapView.toMapPoint(screenPointStart)

       

        let screenPointEnd = CGPoint(x: self.scalebar.frame.origin.x + self.scalebar.frame.size.width, y: self.scalebar.frame.origin.y)

        let mapPointEnd = self.mapView.toMapPoint(screenPointEnd)

       

        let ge = AGSGeometryEngine.defaultGeometryEngine()

        var distance = ge.distanceFromGeometry(mapPointStart, toGeometry: mapPointEnd)

        //the distance calculated here is in metres with the spatial reference GDA 1994 New South Wales Lambert

        if(distance > 10000) //10km

        {

            //show the labels in km

            self.scalebarMiddleLabel.text = String(format:"%0.0f", distance/2000)

            self.scalebarEndLabel.text = String(format:"%0.0f km", distance/1000)

        }

        else

        {

            //show the labels in m

            self.scalebarMiddleLabel.text = String(format:"%0.0f", distance/2)

            self.scalebarEndLabel.text = String(format:"%0.0f m", distance)

        }

    }

Hope it helps.

Shimin

0 Kudos
AkhilChoran_Balan
New Contributor

Thanks for the help Shimin. I will check with this.

0 Kudos