Reputation: 5096
I am trying to figure it out why my google map view only show one marker on the screen even the fetch data got 2 records.I use realm to store latitude and longitude of each position.Even my realm has two markers position,why it drop only one marker,Any help?
@IBOutlet weak var mapView: GMSMapView!
var locationMarker: GMSMarker!
var locationList : Results<TowerLocationList>!
@IBAction func findAddress(sender: AnyObject) {
// Clearing all marker to protect duplicate marker drop
self.mapView.clear()
//Fetching all marker from realm object
locationList = realm.objects(TowerLocationList)
print("The list included at Realm DB is : \(locationList)")
print("List count : \(locationList.count)")
for var i = 0 ; i < locationList.count; i++ {
let coordinate = CLLocationCoordinate2D(latitude: Double(locationList[i].latitude)!, longitude: Double(locationList[i].longitude)!)
self.setupLocationMarker(coordinate)
print("For Loop i : \(i)")
}
}
func setupLocationMarker(coordinate: CLLocationCoordinate2D) {
locationMarker = GMSMarker(position: coordinate)
locationMarker.map = mapView
locationMarker.appearAnimation = kGMSMarkerAnimationPop
locationMarker.icon = UIImage(named: "Radio_Tower")
//locationMarker.opacity = 0.75
locationMarker.flat = true
locationList = realm.objects(TowerLocationList).filter("longitude = '\(coordinate.longitude)' AND latitude = '\(coordinate.latitude)'")
locationMarker.snippet = locationList[0].siteCode
print(locationMarker.snippet)
}
When I click findAddress button, The Output is :
The list included at Realm DB is : Results<TowerLocationList> (
[0] TowerLocationList {
siteCode = AY0121;
longitude = 96.1265519633889;
latitude = 16.8548376155088;
},
[1] TowerLocationList {
siteCode = AY0119;
longitude = 96.1268738284707;
latitude = 16.8490258657804;
}
)
List count : 2
Optional("AY0121")
For Loop i : 0
why the loop stop at index 0 and doesn't go to index 1?
Upvotes: 1
Views: 449
Reputation: 871
try setting this up inside your method for setupLocationMarker(coordinate: CLLocationCoordinate2D)
this is where you will need to place all of your individual markers.
mapView.clear()
for spot in locationList {
let marker = GMSMarker(place: spot)
marker.map = self.mapView
marker.snippet = spot.title
//etc
}
Upvotes: 1