我在将我附近的汽车站标记放在我的Google地图应用程序上时遇到麻烦。
假设我住在纽约市,我想在Google地图上找到所有带标记的汽车站。我尝试了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)
}
}
}
最佳答案
您需要将type
设置为bus_station
的地方搜索调用,以将返回值限制为该特定类型。 response将返回results
及其详细信息,您可以使用它们来设置标记。
关于ios - 在Google map 上放置汽车站标记,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35839522/