Just wondering if this is still not available. Anyone has any solution on how to do this?
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
Thanks for the help Shimin. I will check with this.