Reputation: 444
I am try to get an array of addresses that are coming from webApi and load them on the googleMapView. First I have to convert the address into coordinates and then I am doing that. So I don't know where I am doing.. I am fetching data and I am using model classes... I might be doing this very wrong...
I am beginner as an iOS developer... Somebody please guide me through this...
Code Here:
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
}
}
Upvotes: 0
Views: 52
Reputation: 673
Check my below code you need to get all address from all lat/long. fire Completion handler Once all Address collected.
Create Get Address List Function
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
}
Fire Get All Adress List Function
AllLocationDetected { (LocationArray) in
print(LocationArray)
}
Hope It will help you!
Upvotes: 1