gpt4 book ai didi

ios - 如何在 Google MapView iOS Swift 4 上呈现来自 Web Api 的地址数组

转载 作者:行者123 更新时间:2023-11-29 00:00:05 25 4
gpt4 key购买 nike

我尝试获取一组来自 webApi 的地址并将它们加载到 googleMapView 上。首先,我必须将地址转换为坐标,然后我才这样做。所以我不知道我在做什么......我正在获取数据并且我正在使用模型类......我可能做错了......

我是一名 iOS 开发者初学者......请有人引导我完成这个...

代码在这里:

import GoogleMaps
import GooglePlaces
import Alamofire

class HomeViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,GMSMapViewDelegate,CLLocationManagerDelegate {

var JSONData = [LocationData]()
var serviceNames = [ServiceName]()

override func viewDidLoad() {
super.viewDidLoad()

self.getProfessionalLocations()
}

func getProfessionalLocations() {

let url = "http://domailname.com/api/UsersAPI/GetLocationUsers"

let parameters: NSDictionary = ["Address":""]

Alamofire.request(url, method: .post, parameters: parameters as? [String:Any], encoding: JSONEncoding.default, headers: nil).responseJSON {
response in
switch response.result {
case .success:
print(response)

self.proResponseArray = response.result.value as! NSArray

for eachValue in self.proResponseArray {

let dataValue = eachValue as! [String : Any]
let professionalName = dataValue["userName"] as! String
let imagePath = "http://domailname.com/app/Users/ProfilePics/\(dataValue["profilePic"]!))"
let escapedAddress = imagePath.addingPercentEncoding(
withAllowedCharacters: CharacterSet.urlQueryAllowed)
// self.infoWindow.profile_Img.image = UIImage(url: URL(string: escapedAddress!))

// here I am getting the address from the api
let address = dataValue["searchAddress"] as! String

let services = dataValue["lstServiceProviders"] as! NSArray

for allServiceNames in services {

let names = allServiceNames as! [String : Any]
let serviceName = names["serviceName"] as! String

self.serviceNames.append(ServiceName(serviceName: serviceName))
}

let experience = dataValue["experience"] as! String

let rating = dataValue["rating"] as Any

self.JSONData.append(LocationData(serviceNames: self.serviceNames, proName: professionalName, proImage: escapedAddress!, address: address, experience: experience, rating: rating))
}

for location in self.JSONData {

let marker = GMSMarker()
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(location.address ) { (placemarks, error) in
guard
let placemarks = placemarks,
let addressLocation = placemarks.first?.location
else {
// handle no location found
return
}

marker.position = CLLocationCoordinate2DMake(addressLocation.coordinate.latitude, addressLocation.coordinate.longitude)
marker.icon = UIImage(named:"beautycare")!
self.googleMapView.camera = GMSCameraPosition.camera(withLatitude:addressLocation.coordinate.latitude, longitude: addressLocation.coordinate.longitude, zoom: self.currentZoom)
marker.appearAnimation = GMSMarkerAnimation.pop
marker.map = self.googleMapView
}
}

break
case .failure(let error):

print(error)
}
}
}
class ServiceName {

var serviceName : String

init(serviceName : String) {

self.serviceName = serviceName
}
}

class LocationData {

var serviceNames : [ServiceName]
var proName : String
var proImage : String
var address : String
var experience : String
var rating: Any

init(serviceNames:[ServiceName], proName: String, proImage: String, address : String, experience: String, rating: Any) {

self.serviceNames = serviceNames
self.proName = proName
self.proImage = proImage
self.address = address
self.experience = experience
self.rating = rating
}
}

最佳答案

检查我下面的代码,您需要从所有纬度/经度获取所有地址。触发完成处理程序一旦收集到所有地址。

创建获取地址列表函数

typealias CompletionHandlerButton = (_ response:NSMutableArray) -> Void
func AllLocationDetected(lotLongArray: NSMutableArray,handler: @escaping CompletionHandlerButton)
{
//Write your logic here to Get address from the Lat/long and also need to check all address list collected or not.
handler(ListOfAddress) //Send back your List of address here
}

触发获取所有地址列表功能

AllLocationDetected { (LocationArray) in

print(LocationArray)

}

希望对您有所帮助!

关于ios - 如何在 Google MapView iOS Swift 4 上呈现来自 Web Api 的地址数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49765660/

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