Hello,
I'm building an iOS application that displays the positions of buses on a map for my school. I have an XML file that updates every 10 seconds and has the information of every active bus on campus (ex. route type, latitude, longitude, id, etc.).
My code:
I use NSTimer() to run the function reloadBuses() every 10 seconds.
Inside reloadBuses()
- I display the google map centered on my school
- parse the XML file
- run a for loop that iterates through all the lats and lngs of all the active buses and displays them as markers on the map
My problem is that when the bus get updated with their new positions, the map refreshes as well. I know this happens because the code that displays the map is inside reloadBuses() which gets called every 10 seconds.
I've tried placing the code that displays the map centered on my campus, before NSTimer() (thus, outside of reloadBuses()) and I get an error because the map is out of scope of the code that displays the markers. (line 45: unresolved identifier 'mapView')
/
override func viewDidLoad() {
super.viewDidLoad()
/
let camera = GMSCameraPosition.cameraWithLatitude(37.0000,
longitude: -122.0600, zoom: 14)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.mapType = kGMSTypeNormal
mapView.myLocationEnabled = true
self.view = mapView
/
timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "reloadBuses", userInfo: nil, repeats: true)
}
func reloadBuses() {
/
parser = NSXMLParser(contentsOfURL:(NSURL(string:"http:/
let coord = Coord2()
parser.delegate = coord
parser.parse()
print("coord has a count attribute of \(coord.count)")
print("coord has \(coord.markers.count) markers")
/
/
for marker in coord.markers {
print("marker id=\(marker.id), lat=\(marker.lati), lng=\(marker.lngi), route=\(marker.route)")
/
let buses = GMSMarker()
buses.position = CLLocationCoordinate2DMake(marker.lati, marker.lngi)
buses.title = marker.route
if buses.title == "UPPER CAMPUS" {
buses.icon = UIImage(named: "uppercampus")
} else if buses.title == "LOOP" {
buses.icon = UIImage(named: "innerloop")
}
buses.snippet = marker.id
buses.map = mapView
}
}
I then tried passing 'mapView' into reloadBuses() by doing the following and I got the error:
ViewController reloadBuses(mapView)]: unrecognized selector sent to instance 'XXXX'
/
override func viewDidLoad() {
super.viewDidLoad()
/
let camera = GMSCameraPosition.cameraWithLatitude(37.0000,
longitude: -122.0600, zoom: 14)
let mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.mapType = kGMSTypeNormal
mapView.myLocationEnabled = true
self.view = mapView
/
timer = NSTimer.scheduledTimerWithTimeInterval(10.0, target: self, selector: "reloadBuses(mapView)", userInfo: nil, repeats: true)
}
func reloadBuses(mapDisplay: GMSMapView) {
/
parser = NSXMLParser(contentsOfURL:(NSURL(string:"http:/
let coord = Coord2()
parser.delegate = coord
parser.parse()
print("coord has a count attribute of \(coord.count)")
print("coord has \(coord.markers.count) markers")
/
/
for marker in coord.markers {
print("marker id=\(marker.id), lat=\(marker.lati), lng=\(marker.lngi), route=\(marker.route)")
/
let buses = GMSMarker()
buses.position = CLLocationCoordinate2DMake(marker.lati, marker.lngi)
buses.title = marker.route
if buses.title == "UPPER CAMPUS" {
buses.icon = UIImage(named: "uppercampus")
} else if buses.title == "LOOP" {
buses.icon = UIImage(named: "innerloop")
}
buses.snippet = marker.id
buses.map = mapDisplay
}
}
I want to be able to update the buses' positions without having to refresh the map.