Im running a web map on my ios app using Swift but am unable to get the current location once the map loads. I've also made sure to add NSLocationWhenInUseUsageDescription in the Info.plist file. Below is the code I'm working with.
import UIKit
import ArcGIS
import CoreLocation
class MapViewController: UIViewController, AGSCalloutDelegate, AGSMapViewLayerDelegate, AGSWebMapDelegate, CLLocationManagerDelegate
{
@IBOutlet weak var mapView: AGSMapView!
let webMap = AGSWebMap(itemId: "f553d4b86e354c05917ad269de26f6f0", credential: nil)
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.webMap.openIntoMapView(self.mapView)
self.webMap.delegate = self
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.requestWhenInUseAuthorization()
}
func webMapViewDidLoad(mapView: AGSWebMap!) {
self.mapView.locationDisplay.startDataSource()
}
func webMapDidLoad(webMap: AGSWebMap!) {
print("Web Map Loaded")
}
func didOpenWebMap(webMap: AGSWebMap!, intoMapView mapView: AGSMapView!) {
print("Opened the Web Map into the Map View")
}
func webMap(webMap: AGSWebMap!, didLoadLayer layer: AGSLayer!) {
print("Loaded layer \(layer.name)")
}
func webMapView(webMap: AGSWebMap!, didFailToLoadWithError error: NSError!) {
print("Layer failed to load into Web Map! \(error.localizedDescription)")
}
}
Any help would be greatly appreciated!
Can you remove the `locationManager` code, delete the app and install again. And see if you get asked for location authorization?
I deleted the app off the simulator and cleaned it. Not getting any results yet. Below is my updated code.
import UIKit
import ArcGIS
import CoreLocation
class MapViewController: UIViewController, AGSCalloutDelegate, AGSMapViewLayerDelegate, AGSWebMapDelegate,CLLocationManagerDelegate
{
@IBOutlet weak var mapView: AGSMapView!
let webMap = AGSWebMap(itemId: "f553d4b86e354c05917ad269de26f6f0", credential: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.webMap.openIntoMapView(self.mapView)
self.webMap.delegate = self
}
func webMapViewDidLoad(mapView: AGSWebMap!) {
self.mapView.locationDisplay.startDataSource()
}
func webMapDidLoad(webMap: AGSWebMap!) {
print("Web Map Loaded")
}
func didOpenWebMap(webMap: AGSWebMap!, intoMapView mapView: AGSMapView!) {
print("Opened the Web Map into the Map View")
}
func webMap(webMap: AGSWebMap!, didLoadLayer layer: AGSLayer!) {
print("Loaded layer \(layer.name)")
}
func webMapView(webMap: AGSWebMap!, didFailToLoadWithError error: NSError!) {
print("Layer failed to load into Web Map! \(error.localizedDescription)")
}
}
1. Can you check if the control is going inside `webMapViewDidLoad` method? You can put a breakpoint at that line in debug mode or simply put a NSLog statement.
2. When you using CLLocationManager code below, did you get the authorization alert in the app, the first time you ran the app?
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
locationManager.requestWhenInUseAuthorization()
3. What version of the ArcGIS SDK are you using?
Hi Gina,
Make sure you test with this on a physical machine and sometimes the simulator may not show up correctly with location. Here is the video clip that shows it works with my physical device: Webmap with locationDisplay.
Here I shared with you the code in that video:
import UIKit
import ArcGIS
class ViewController: UIViewController , AGSWebMapDelegate, AGSMapViewLayerDelegate {
@IBOutlet weak var mapView: AGSMapView!
var currentLocation: AGSLocation!
let webmap = AGSWebMap(itemId: "f553d4b86e354c05917ad269de26f6f0", credential: nil)
override func viewDidLoad() {
super.viewDidLoad()
self.webmap.delegate = self
self.webmap.openIntoMapView(self.mapView)
self.mapView.locationDisplay.startDataSource()
}
func webMapDidLoad(webMap: AGSWebMap!) {
print("Load webmap!")
}
func webMap(webMap: AGSWebMap!, didLoadLayer layer: AGSLayer!) {
currentLocation = self.mapView.locationDisplay.location
print(currentLocation)
let projectedMapPoint = AGSGeometryEngine.defaultGeometryEngine().projectGeometry(currentLocation.point, toSpatialReference: AGSSpatialReference.webMercatorSpatialReference()) as! AGSPoint;
self.webmap.mapView.centerAtPoint(projectedMapPoint, animated: true)
}
func webMap(webMap: AGSWebMap!, didFailToLoadWithError error: NSError!) {
print("Error while loading webmap: \(error.localizedDescription)")
}
}
If you really need to make it work in simulator, make sure to "Configuring a Project".
Bring up your project's scheme editor by navigating to Product -> Scheme -> Edit Scheme in menu bar. In the scheme's Run action, select the Options tab. You should see something similar to this:
Hope this can help.