我在谷歌地图应用程序上放置靠近我的公交车站的标记时遇到了麻烦。
假设我住在纽约市,我想在我的谷歌地图上标记所有巴士站。但是,我真的不知道如何获取我当前城市(纽约)的所有巴士站数据并在其上放置一个选择器。我尝试了AppCoda教程,它只给我自定义搜索的标记
此处:http://www.appcoda.com/google-maps-api-tutorial/
有什么方法可以修改它的" MapTask.Swift"代码让我附近的所有公共汽车站和每个公交车站都有选择器?
这是MapTask.Swift
import UIKit
import CoreLocation
import SwiftyJSON
class MapTask: NSObject {
let baseURLGeocode = "https://maps.googleapis.com/maps/api/geocode/json?"
var lookupAddressResults: Dictionary<NSObject, AnyObject>!
var fetchedFormattedAddress: String!
var fetchedAddressLongitude: Double!
var fetchedAddressLatitude: Double!
override init() {
super.init()
}
func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) {
if let lookupAddress = address {
var geocodeURLString = baseURLGeocode + "address=" + lookupAddress
geocodeURLString = geocodeURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let geocodeURL = NSURL(string: geocodeURLString)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)
let dictionary: Dictionary<NSObject, AnyObject> = try!NSJSONSerialization.JSONObjectWithData(geocodingResultsData!, options: NSJSONReadingOptions.MutableContainers) as! Dictionary<NSObject, AnyObject>
// Get the response status.
let status = dictionary["status"] as! String
if status == "OK" {
let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>>
self.lookupAddressResults = allResults[0]
print("The result is : \(dictionary)")
// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as! String
let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
completionHandler(status: status, success: true)
}
else {
completionHandler(status: status, success: false)
}
})
}
else {
completionHandler(status: "No valid address.", success: false)
}
}
}