本文介绍了Swift 4:无法显示多个标记的Google Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing an app that have a multiple location/marker.

我创建了一个结构

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...
]

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

and try a looping but the marker did not display

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
    }

当我喜欢以下内容时,它正在工作.但我知道这不是更好的方法

when i do like below, it is working. but i know it's not a better way

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

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

Does anyone know what is wrong in my code?

推荐答案

您在第一次尝试中就将lat和Lon交换了下来,由于经度是102.2989,它在第二秒就起作用了,但是首先您交换了它们

You swapped the lat and Lon in first try , it works in second as longitude is 102.2989 , but in first you swapped them

 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°

Latitude measurements range from 0° to (+/–)90°. Longitude measures how far east or west of the prime meridian a place is located. The prime meridian runs through Greenwich, England. Longitude measurements range from 0° to (+/–)180°

这篇关于Swift 4:无法显示多个标记的Google Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:21