Hi all,
I want to save viewpoint coordinates when a user sees map in online mode and want to load tpk file with same viewpoint coordinates where a user has the last zoom in/out on the online map. Currently, I am using below handler to get viewpoint coordinates:
//handler call when user zoomin , zoomout
self.mapView.viewpointChangedHandler = { [weak self] () in
----> //how to get viewpoint coordinates here
}
How do I get viewpoint coordinates, zoom level. Is there any property or method in swift.
Currently i am using ArcGIS runtime IOS sdk 100.0 in swift language.
Thanks is advance,
Kamal
Hi Kamal Mittal
Used this sample (https://github.com/Esri/tips-and-tricks-ios/blob/master/DevSummit2015_TipsAndTricksDemos/Tips-And-Tr...) to implement observer on KVO for MapView.navigating property in version 100.0.0.
Here is a snippet code that you can implement in 100.0.0: AGSMapView navigating
@IBOutlet weak var mapView: AGSMapView!
private weak var map: AGSMap!
override func viewDidLoad() {
super.viewDidLoad()
let map = AGSMap(basemap: AGSBasemap.imageryBasemap())
self.mapView.map = map
self.mapView.addObserver(self, forKeyPath: "navigating",
options: NSKeyValueObservingOptions.New, context: nil)
}
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?,
change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
print("trigger")
//code implementation
}
Hope this can help.
When you go to open the second map with the tpk, you just need to use the same viewpoint you currently have. You just ask the mapView for the currentViewpoint, then when you open the second map you set the initial viewpoint of it to that previous viewpoint.
// get current viewpoint
let vp = mapView.currentViewpoint(with: .centerAndScale)
// if you want to see the coordinates of it, you do that like so
print("viewpoint scale: \(vp?.targetScale)")
print("viewpoint center: \(vp?.targetGeometry as! AGSPoint)")
let map2 = AGSMap()
// add tpk layers, etc here
// ...
// ...
//
// when you open the second map set the initial viewpoint to the
// viewpoint that you saved
map2.initialViewpoint = vp
// open second map
mapView.map = map2
Thanks Ryan
I am able to get viewpoint coordinates but when I trying to save AGSViewPoint object in cache/localstorage then application is crashing at runtime. Below code which I am using:
struct defaultsKeys {
static var keyViewPoint = "keyViewPoint"
}
self.mapView.viewpointChangedHandler = { [weak self] () in
self?.viewPoint = self?.mapView.currentViewpointWithType(AGSViewpointType.CenterAndScale)
let defaults = NSUserDefaults.standardUserDefaults()
defaults.set(self?.viewPoint, forKey: defaultsKeys.keyViewPoint) // Here I am getting error
defaults.synchronize()
}
if let mapKeyViewPoint = defaults.stringForKey(defaultsKeys.keyViewPoint) {
}
Thanks,
Kamal
The user defaults doesn't take just any object. You have a few options. One option is that you can put the JSON Dictionary representation of the view point in the user defaults using the toJSON method. This is what I think you should do. Then when you pull it out of the user defaults, use the fromJSON. AGSViewpoint is AGSJSONSerializable and has the to/fromJSON methods.