【问题标题】:GoogleMapView showing only one marker even its has 2 or moreGoogleMapView 仅显示一个标记,即使它有 2 个或更多
【发布时间】:2016-06-23 06:52:35
【问题描述】:

我试图弄清楚为什么我的谷歌地图视图只在屏幕上显示一个标记,即使获取数据有 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?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 我试过了,还是不行。这里:puu.sh/nzXZK/2a50a5a6e8.png。只显示一个标记,我怎么了?;(
    • 我现在知道了:puu.sh/nzYiv/59cfe753fa.png。我可以知道有什么区别吗?
    • 介意解释一下吗?为什么它在正常的for循环而不是每个循环中只放置一个标记?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-05
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多