如何在 GoogleMapView iOS Swift4 上显示来自 Web Api 的地址数组
How to present Array Of addresses data from Web Api On GoogleMapView iOS Swift4
我正在尝试获取一组来自 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
}
}
检查我下面的代码,您需要从所有 lat/long 获取所有地址。触发完成处理程序一旦收集到所有地址。
创建获取地址列表函数
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)
}
希望对您有所帮助!
我正在尝试获取一组来自 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
}
}
检查我下面的代码,您需要从所有 lat/long 获取所有地址。触发完成处理程序一旦收集到所有地址。
创建获取地址列表函数
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)
}
希望对您有所帮助!