简体   繁体   English

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

[英]I want place multiple markers using JSON with google maps SDK

I'm trying to add multiple markers by using google maps SDK.i get data but multiple markers are not inserted please any help welcome 我正在尝试通过使用Google Maps SDK添加多个标记。我获取数据,但未插入多个标记,欢迎任何帮助

I try following code: 我尝试以下代码:

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()
}

I get exception like : 我得到像这样的异常:

2017-04-27 12:08:26.207 Inte-GoogleMaps[759:15071] This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. 2017-04-27 12:08:26.207 Inte-GoogleMaps [759:15071]从主线程访问引擎后,此应用程序正在从后台线程修改自动布局引擎。 This can lead to engine corruption and weird crashes. 这会导致引擎损坏和奇怪的崩溃。

Terminating app due to uncaught exception 'GMSThreadException', reason: 'The API method must be called from the main thread' 由于未捕获的异常“ GMSThreadException”而终止应用程序,原因:“必须从主线程调用API方法”

The assignment self.view = map view is outside your main queue dispatch queue block (and hence being run on a background thread). 分配self.view = map view在主队列调度队列块之外(因此在后台线程上运行)。 This code changes the state of a view and therefore must run on the main thread or it will cause problems for the UI engine (all iOS UI code must run on the main thread). 此代码更改视图的状态,因此必须在主线程上运行,否则将导致UI引擎出现问题(所有iOS UI代码必须在主线程上运行)。

This is why you are seeing the warning about running Autolayout on a background thread. 这就是为什么您会看到有关在后台线程上运行自动布局的警告的原因。

You have the same line commented out in the DispatchQueue.main block above. 在上面的DispatchQueue.main块中,您注释了同一行。 This is the correct place for it, so you were obviously already thinking along the right lines! 这是正确的地方,因此您显然已经在按正确的思路进行思考!

You should delete the line from its current position, and uncomment the commented line. 您应该从当前位置删除该行,并取消注释该行。 I've run your code on my machine with this change, and it works ok - it adds a bunch of markers around the area of Pyrmont in Sydney. 进行此更改后,我已经在计算机上运行了您的代码,并且效果很好-在悉尼的Pyrmont地区周围添加了许多标记。

You also need to change the code that centers the map - you are missing a minus sign in front of the latitude - as posted it zooms to somewhere off the coast of Japan. 您还需要更改以地图为中心的代码-您在纬度前面缺少减号-因为它会放大到日本沿海某个地方。 You probably also want to zoom in a bit - zoom level 15 looks good on my simulator. 您可能还想放大一点-缩放级别15在我的模拟器上看起来不错。

The camera position code should be: 相机位置代码应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM