【问题标题】:I want place multiple markers using JSON with google maps SDK我想使用带有谷歌地图 SDK 的 JSON 放置多个标记
【发布时间】:2017-04-27 19:24:45
【问题描述】:

我正在尝试使用谷歌地图 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] 在从主线程访问引擎后,此应用程序正在从后台线程修改自动布局引擎。这可能会导致引擎损坏和奇怪的崩溃。

由于未捕获的异常“GMSThreadException”而终止应用程序,原因:“必须从主线程调用 API 方法”

【问题讨论】:

    标签: ios iphone


    【解决方案1】:

    分配self.view = map view 在您的主队列调度队列块之外(因此在后台线程上运行)。此代码会更改视图的状态,因此必须在主线程上运行,否则会导致 UI 引擎出现问题(所有 iOS UI 代码必须在主线程上运行)。

    这就是您看到有关在后台线程上运行 Autolayout 的警告的原因。

    您在上面的 DispatchQueue.main 块中注释掉了同一行。这是它的正确位置,所以你显然已经在沿着正确的路线思考!

    您应该从当前位置删除该行,并取消注释已注释的行。我已经通过这个更改在我的机器上运行了你的代码,它工作正常 - 它在悉尼的 Pyrmont 地区周围添加了一堆标记。

    您还需要更改使地图居中的代码 - 您在纬度前面缺少一个减号 - 因为它会缩放到日本海岸附近的某个地方。您可能还想放大一点 - 缩放级别 15 在我的模拟器上看起来不错。

    相机位置代码应该是:

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

    【讨论】:

    • 嗨@RichTolley 你拯救了我的一天。比你。
    • @D.Ramana 没问题,如果有帮助,您介意接受答案吗?
    猜你喜欢
    • 2018-09-28
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多