【问题标题】:Swift 4: multiple marker Google Map not displaySwift 4:多标记谷歌地图不显示
【发布时间】:2018-02-27 08:09:47
【问题描述】:

我正在开发一个具有多个位置/标记的应用程序。

我创建了一个结构

let states = [
    State(name: "Zoo 1", long: 2.276537, lat: 102.2989),
    State(name: "Zoo 2", long: 2.2772141, lat: 102.2984333),
    // the other 51 states here...
]

并尝试循环,但标记未显示

for state in states {
        let state_marker = GMSMarker()
        state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
        state_marker.title = state.name
        state_marker.snippet = "Hey, this is \(state.name)"
        state_marker.map = mapView
    }

当我喜欢下面时,它正在工作。但我知道这不是更好的方法

let state_marker = GMSMarker()
    state_marker.position = CLLocationCoordinate2D(latitude: 2.276622, longitude: 102.2989)
    state_marker.title = "Zoo 1"
    state_marker.snippet = "Hey, this is Zoo 1"
    state_marker.map = mapView

    let state_marker1 = GMSMarker()
    state_marker1.position = CLLocationCoordinate2D(latitude: 2.2772141, longitude: 102.2984333)
    state_marker1.title = "Zoo 2"
    state_marker1.snippet = "Hey, this is Zoo 2"
    state_marker1.map = mapView

有人知道我的代码有什么问题吗?

【问题讨论】:

标签: ios swift google-maps


【解决方案1】:

你在第一次尝试时交换了 lat 和 Lon,它在第二个工作,因为经度是 102.2989,但首先你交换了它们

 for state in states {
    let state_marker = GMSMarker()
    state_marker.position = CLLocationCoordinate2D(latitude: state.lat, longitude: state.long)
    state_marker.title = state.name
    state_marker.snippet = "Hey, this is \(state.name)"
    state_marker.map = mapView
}

所以把数组改成这个

let states = [
    State(name: "Zoo 1", long:102.2989 , lat:  2.276537),
    State(name: "Zoo 2", long:102.2984333 , lat:  2.2772141),
    // the other 51 states here...
]

编辑:

纬度测量范围从 0° 到 (+/-)90°。经度衡量一个地方位于本初子午线以东或以西的距离。本初子午线穿过英国格林威治。经度测量范围从 0° 到 (+/-)180°

【讨论】:

  • 嗯...我没有看到任何偏离他或她的地方。
  • 仔细查看参数,他在块中交换的值不起作用
  • 它的工作。我可以知道,为什么我们要交换经度和纬度?
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 2019-10-10
  • 1970-01-01
相关资源
最近更新 更多