gpt4 book ai didi

ios - 为什么谷歌地图上不显示多个标记

转载 作者:行者123 更新时间:2023-11-28 15:44:53 24 4
gpt4 key购买 nike

我正在尝试在谷歌地图上显示多个标记。但没有加载。这是我的代码。我将 Alamofire 与 SwiftyJSON 结合使用。

class ViewController: UIViewController { 
var Location: [Place] = []
override func viewDidLoad() {
super.viewDidLoad()
_ = [Int : GMSMarker]()
let camera = GMSCameraPosition.camera(withLatitude: 0, longitude: 99, zoom: 6)
_ = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
self.getInfoSchedule()
}

这是我从 json 解析数据并尝试使用 map 的函数:

func getInfoSchedule() {
var marker = [Int : GMSMarker]()
Alamofire.request("https://aqueous-depths-77407.herokuapp.com/earthquakes.json", method: .get, parameters: nil, encoding: URLEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success(let value):
let json = JSON(value)
var i=0
for counter in json.arrayValue {
let lat = counter["latitude"].doubleValue
let long = counter["longitude"].doubleValue
let name = counter["place"].stringValue
let type = counter["usgs_ident"].stringValue
self.Location.append(Place(longitude:long,latitude:lat,name:name,type:type))
print("latitude \(lat)")
marker[i] = GMSMarker()
marker[i]?.position = CLLocationCoordinate2DMake(lat, long)
marker[i]?.snippet = "Latitude: \(lat) Longitude: \(long)"
marker[i]?.map = mapView
marker[i]?.icon = GMSMarker.markerImage(with: UIColor.purple)
i += 1
print(i)
}
self.view = mapView
case .failure(let error):
print("Error: \(error)")
}}}}
struct Place {
var longitude:Double
var latitude:Double
var name:String
var type:String
}

最佳答案

回答“为什么谷歌地图上不显示多个标记?”这个问题- 这是因为你只是在制作 for 循环 但没有使用它的索引,你正在制作一些其他变量 i = 0 然后在循环结束后将值 1 赋给它完成。

我会写一些更长的答案,因为 map 是新趋势,许多其他人可能也会发现它有帮助。我花了很多时间来让这样的东西发挥作用。如果您想快速解决您的问题,请滚动并查看第 4 步。

我应该做的是:

1 - 使用您希望从服务器获取的信息创建对象:

class Place: NSObject {
var longitude: Double
var latitude: Double
var name: String
var type: String

init(longitude: Double, latitude: Double, name: String, type: String) {
self.longitude = longitude
self.latitude = latitude
self.name = name
self.type = type
}


init?(dict: [String: JSON]) {
guard let longitude = dict["longitude"]?.doubleValue, let latitude = dict["latitude"]?.doubleValue, let name = dict["place"]?.stringValue, let type = dict["usgs_ident"]?.stringValue
else { return nil }

self.longitude = longitude
self.latitude = latitude
self.name = name
self.type = type
}
}

2 - 创建路由器。这不是必需的,但可以使它更清晰,并且可以轻松地在 header 中使用 token :

enum Router: URLRequestConvertible{
case getAllPlaces

static let baseURLString = "https://aqueous-depths-77407.herokuapp.com"//This is your base url

var method: HTTPMethod{
switch self {
case .getAllPlaces:
return .get
default:
return HTTPMethod(rawValue: "Could not determine method")!
}
}

var path: String{
switch self {
case .getAllPlaces:
return "/earthquakes.json"

default:
return "Could not determine route"
}
}

// MARK: URLRequestConvertible

func asURLRequest() throws -> URLRequest{
let url = try Router.baseURLString.asURL()

var urlRequest = URLRequest(url: url.appendingPathComponent(path))
urlRequest.httpMethod = method.rawValue

if let token = Util.getApiToken(){
urlRequest.setValue("bearer \(token)", forHTTPHeaderField: "Authorization")
}



return urlRequest
}
}

3 - 在您处理请求的位置创建扩展,例如 API+Places.swift:

extension API{

//YOUR INFO
class func getInfo(completion: @escaping (_ error: Error?, _ place: [Place]?)->Void) {
Alamofire.request(Router.getAllPlaces().responseJSON{ response in

switch response.result{

case .failure(let error):
completion(error, nil)
print(error)

case .success(let value):
let json = JSON(value)
print(json)

guard let dataArr = json["data"].array else {
completion(nil, nil)
return
}

var info = [Place]()

for data in dataArr {
if let data = data.dictionary, let info = Place.init(dict: data) {
info.append(info)
}

}

completion(nil, info)

}
}

}
}

4 - 最后得到信息:

创建对象数组:var placesArray = [Place]()

调用你的函数:

API.getInfo(){
(error, inf: [Place]?) in
if let info = inf{
self.placesArray = info
}
for location in self.placesArray{
let marker = GMSMarker()
annotation.snippet = "\(location.latitude) \(location.longitude)"
annotation.position = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
annotation.map = self.mapView
}
}
self.view = self.mapView

关于ios - 为什么谷歌地图上不显示多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43266940/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com