我想使用与Google Maps SDK一起使用JSON放置多个标记



我正在尝试使用Google Maps SDK添加多个标记。我获取数据,但未插入多个标记,请欢迎任何帮助

我尝试以下代码:

import UIKit
import GoogleMaps
import GooglePlaces
class ViewController: UIViewController,GMSMapViewDelegate {

override func viewDidAppear(_ animated: Bool) {
    let getPlaces: String = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&type=restaurant&key=AIzaSyCNKHyGGXXfsRq6bsiG6EmtQiy7ApN2TFg"
    let frame = GMSCameraPosition.camera(withLatitude: 33.8670522, longitude: 151.1957362, zoom: 12.0)
    let mapview = GMSMapView.map(withFrame: self.view.bounds, camera: frame)

    let session = URLSession.shared.dataTask(with: URL(string: getPlaces)!) { (data, response, error) in
        do{
            let jsonResult = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
            // print(jsonResult)

            let returnedPlaces: NSArray? = jsonResult["results"] as? NSArray
            if returnedPlaces != nil {
                for index in 0..<returnedPlaces!.count {
                    if let returnedPlace = returnedPlaces?[index] as? NSDictionary {
                        var placeName = ""
                        var latitude = 0.0
                        var longitude = 0.0

                        if let name = returnedPlace["name"] as? NSString {
                            placeName = name as String
                        }
                        if let geometry = returnedPlace["geometry"] as? NSDictionary{
                            if let location = geometry["location"] as? NSDictionary {
                                if let lat = location["lat"] as? Double {
                                    latitude = lat
                                }
                                if let lng = location["lng"] as? Double {
                                    longitude = lng
                                }
                            }
                        }
                        DispatchQueue.main.async{
                            // let marker = index
                            let marker = GMSMarker()
                            marker.position = CLLocationCoordinate2DMake(latitude, longitude)
                            marker.title = placeName
                           // self.view = mapview
                            marker.map = mapview
                            print("HI see this (placeName)")
                        }
                    }

                }
                self.view = mapview
             }
        }catch
        {
            print("Error")
        }
    }
    session.resume()
}

我得到了例外,例如:

2017-04-27 12:08:26.207 inte-googlemaps [759:15071]此应用程序正在从主线程访问引擎后从背景线程中修改Autolayout引擎。这可能导致引擎损坏和怪异的崩溃。

终止由于未被发现的例外" gmsthreadexception",原因是:"必须从主线程调用API方法"

分配self.view = map view在您的主队列派遣队列块之外(因此在背景线程上运行(。此代码更改视图的状态,因此必须在主线程上运行,否则会引起UI引擎的问题(所有iOS UI Code 必须在主线程上运行(。

这就是为什么您看到有关在背景线程上运行AutoLayout的警告的原因。

您在上面的DispatchQueue.main块中注释了相同的行。这是正确的地方,因此您显然已经在正确的线路上思考!

您应该从当前位置删除该线,并取消注释的行。我使用此更改在计算机上运行您的代码,并且可以正常运行 - 它在悉尼皮尔蒙特(Pyrmont(区域周围增加了一堆标记。

您还需要更改以地图为中心的代码 - 您缺少在纬度前面的减号 - 张贴在日本海岸附近的某个地方。您可能还想放大一点-Zoom Level 15在我的模拟器上看起来不错。

相机位置代码应为:

let frame = GMSCameraPosition.camera(withLatitude: -33.8670522, longitude: 151.1957362, zoom: 15.0)

最新更新