GoogleMapView只显示一个标记,即使它有2个或更多

时间:2016-03-09 03:37:14

标签: swift google-maps google-maps-markers

我想弄明白为什么我的谷歌地图视图只在屏幕上显示一个标记,即使获取数据有2条记录。我使用领域来存储每个位置的纬度和经度。即使我的领域有两个标记位置,为什么它只丢弃一个标记,任何帮助?

@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)

}

当我点击findAddress按钮时, 输出是:

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

为什么循环在索引0处停止并且没有转到索引1?

1 个答案:

答案 0 :(得分:1)

尝试在setupLocationMarker(coordinate: CLLocationCoordinate2D)的方法中进行设置,这是您需要放置所有单个标记的地方。

mapView.clear()
for spot in locationList {
    let marker = GMSMarker(place: spot)
    marker.map = self.mapView
    marker.snippet = spot.title 
    //etc
}