使用Google iOS SDK创建多个标记 [英] Create multiple markers using Google iOS SDK

查看:112
本文介绍了使用Google iOS SDK创建多个标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是斯威夫特的新手。我在谷歌地图上得到2个标记:

I am a newbie in Swift. I was ale to get 2 markers on Google Maps:

import UIKit
import GoogleMaps

class ViewController: UIViewController {

    // You don't need to modify the default init(nibName:bundle:) method.

    override func loadView() {
        let camera = GMSCameraPosition.cameraWithLatitude(37.0902, longitude: -95.7129, zoom: 3.0)
        let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
        mapView.myLocationEnabled = true
        view = mapView

        let state_marker = GMSMarker()
        state_marker.position = CLLocationCoordinate2D(latitude: 61.370716, longitude: -152.404419)
        state_marker.title = "Alaska"
        state_marker.snippet = "Hey, this is Alaska"
        state_marker.map = mapView

        let state_marker1 = GMSMarker()
        state_marker1.position = CLLocationCoordinate2D(latitude: 32.806671, longitude: -86.791130)
        state_marker1.title = "Alabama"
        state_marker1.snippet = "Hey, this is Alabama"
        state_marker1.map = mapView

    }
}

我需要为不同的l添加51个标记具有不同标题和片段的每个州的经度和经度。

I need to add 51 more markers to different latitude and longitude for each state with different title and snippet.

我可以将此块复制51次,但有没有办法优化此代码?

I can probably just copy this block 51 times but is there a way to optimize this code?

推荐答案

你应该创建一个这样的结构:

You should create a struct like this:

struct State {
    let name: String
    let long: CLLocationDegrees
    let lat: CLLocationDegrees
}

然后在VC中创建此结构的数组:

Then create an array of this struct in your VC:

let states = [
    State(name: "Alaska", long: -152.404419, lat: 61.370716),
    State(name: "Alabama", long: -86.791130, lat: 32.806671),
    // the other 51 states here...
]

现在你可以循环通过数组,在每次迭代中添加标记:

Now you can just loop through the array, adding markers in each iteration:

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
}

您可能还想添加一个字典,将状态名称存储为键和相应的 GMSMarker 作为值。这样,您可以稍后修改标记。

You might also want to add a dictionary that stores the names of the states as keys and the corresponding GMSMarker as value. This way, you can modify the markers later.

var markerDict: [String: GMSMarker] = [:]

override func loadView() {

    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
        markerDict[state.name] = state_marker
    }

}

这篇关于使用Google iOS SDK创建多个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆