我尝试获取来自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
}
}
答案 0 :(得分:1)
检查我需要的以下代码,以获取所有纬度/经度的所有地址。 fire完成处理程序收集所有地址后。
创建获取地址列表功能
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)
}
希望它会对你有帮助!