Ios 我想在google maps SDK中使用JSON放置多个标记
我正在尝试使用google maps SDK添加多个标记。我获得了数据,但没有插入多个标记。请提供任何帮助,欢迎 我尝试以下代码:Ios 我想在google maps SDK中使用JSON放置多个标记,ios,iphone,Ios,Iphone,我正在尝试使用google maps SDK添加多个标记。我获得了数据,但没有插入多个标记。请提供任何帮助,欢迎 我尝试以下代码: import UIKit import GoogleMaps import GooglePlaces class ViewController: UIViewController,GMSMapViewDelegate { override func viewDidAppear(_ animated: Bool) { let getPlaces: Str
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()
}
导入UIKit
导入谷歌地图
导入谷歌网站
类ViewController:UIViewController、GMSMapViewDelegate{
覆盖函数视图显示(u动画:Bool){
让getPlaces:String=”https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522151.1957362&radius=500&type=restaurant&key=aizasynkhyggxxfsrq6bsig6emtqiy7apn2tfg“
设帧=GMSCameraPosition.camera(纬度:33.8670522,经度:151.1957362,变焦:12.0)
让mapview=GMSMapView.map(带边框:self.view.bounds,相机:边框)
让session=URLSession.shared.dataTask(带:URL(string:getPlaces)!){(数据、响应、错误)在
做{
让jsonResult=try JSONSerialization.jsonObject(使用:data!,选项:JSONSerialization.ReadingOptions.mutableContainers)作为!NSDictionary
//打印(jsonResult)
让returnedPlaces:NSArray?=jsonResult[“results”]作为?NSArray
如果返回位置!=零{
对于0..中的索引,分配self.view=map view
在主队调度队列块之外(因此在后台线程上运行)。此代码更改视图的状态,因此必须在主线程上运行,否则将导致UI引擎出现问题(所有iOS UI代码必须在主线程上运行)
这就是为什么您会看到关于在后台线程上运行Autolayout的警告
您在上面的DispatchQueue.main
块中注释了同一行。这是正确的位置,因此您显然已经按照正确的思路思考了
您应该从当前位置删除该行,并取消注释注释该行。我已经在我的计算机上运行了您的代码,并进行了此更改,它工作正常-它在悉尼的皮尔蒙特地区周围添加了一组标记
你还需要更改地图中心的代码-你在纬度前面缺少一个负号-因为它会缩放到日本海岸附近的某个地方。你可能还想放大一点-缩放级别15在我的模拟器上看起来不错
摄像机位置代码应为:
let frame=GMSCameraPosition.camera(纬度:-33.8670522,经度:151.1957362,缩放:15.0)
分配self.view=map view
在主队列调度队列块之外(因此在后台线程上运行)。此代码更改视图的状态,因此必须在主线程上运行,否则将导致UI引擎出现问题(所有iOS UI代码必须在主线程上运行)
这就是为什么您会看到关于在后台线程上运行Autolayout的警告
您在上面的DispatchQueue.main
块中注释了同一行。这是正确的位置,因此您显然已经按照正确的思路思考了
您应该从当前位置删除该行,并取消注释注释该行。我已经在我的计算机上运行了您的代码,并进行了此更改,它工作正常-它在悉尼的皮尔蒙特地区周围添加了一组标记
你还需要更改地图中心的代码-你在纬度前面缺少一个负号-因为它会缩放到日本海岸附近的某个地方。你可能还想放大一点-缩放级别15在我的模拟器上看起来不错
摄像机位置代码应为:
let frame=GMSCameraPosition.camera(纬度:-33.8670522,经度:151.1957362,变焦:15.0)
你好@richtoley你救了我一天。比你多。@D.Ramana没问题,如果答案有用,你介意接受吗。你好@richtoley你救了我一天。比你多。@D.Ramana没问题,如果答案有用,你介意接受吗。