【问题标题】:Placing marker of bus station on googlemap在谷歌地图上放置公交车站的标记
【发布时间】:2016-03-07 08:50:12
【问题描述】:

我无法在我的谷歌地图应用程序上放置我附近的公交车站标记。

假设我住在纽约市,我想在我的谷歌地图上所有带有标记的公交车站。但是,我真的不知道如何获取我当前城市(纽约)和地点的所有公交车站数据一个选择器。我尝试了 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)
    }
}

}

【问题讨论】:

    标签: ios swift google-maps marker


    【解决方案1】:

    您需要调用 Place Search 并将 type 设置为 bus_station 以限制返回到该特定类型。 response 将返回 results 及其可用于设置标记的详细信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 1970-01-01
      • 1970-01-01
      • 2012-06-05
      • 2017-12-29
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多