无法在 Google 地图中建立自行车路线
Unable to build a bicycle route in Google maps
我很奇怪!当我使用 origin1 和 destinationA 不工作时
status = "ZERO_RESULTS";
但如果使用 origin2 和 destinationB 它工作完美我错过了什么
var origin = "55.930385, -3.118425"
var destination = "50.087692, 14.421150"
override func viewDidLoad() {
super.viewDidLoad()
durationDestance(origin: origin, destination: destination, mode:"bicycling")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
let session = URLSession.shared
let url = URL(string:urlString)!
session.dataTask(with: url) { (data: Data?, response: URLResponse?, erorr: Error?) -> Void in
print("url: \(urlString)")
if let responseData = data {
do{
let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
因为这些点之间没有自行车道。
尝试select自行车:google maps
I tried to make a route by bike and car. It is impossible to build a bicycle route, and the route by car exists
Swift 3 个完整样本
import UIKit
class ViewController: UIViewController {
var origin = "55.930385, -3.118425"
var destination = "50.087692, 14.421150"
override func viewDidLoad() {
super.viewDidLoad()
durationDestance(origin: origin, destination: destination, mode:"bicycling")
durationDestance(origin: origin, destination: destination, mode:"auto")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
let session = URLSession.shared
let url = URL(string:urlString)!
session.dataTask(with: url) { (data: Data?, response: URLResponse?, erorr: Error?) -> Void in
print("url: \(urlString)")
if let responseData = data {
do{
let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
}
Swift 2.3 全样本
import UIKit
class ViewController: UIViewController {
var origin = "55.930385, -3.118425"
var destination = "50.087692, 14.421150"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
durationDestance(origin, destination: destination, mode:"bicycling")
durationDestance(origin, destination: destination, mode:"auto")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!
let session=NSURLSession.sharedSession();
let url=NSURL(string:urlString)!
session.dataTaskWithURL(url) { (data: NSData?,response: NSURLResponse?,erorr :NSError?) -> Void in
print("url: \(urlString)")
if let responseData=data{
do{
let json=try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
}
结果
我很奇怪!当我使用 origin1 和 destinationA 不工作时
status = "ZERO_RESULTS";
但如果使用 origin2 和 destinationB 它工作完美我错过了什么
var origin = "55.930385, -3.118425"
var destination = "50.087692, 14.421150"
override func viewDidLoad() {
super.viewDidLoad()
durationDestance(origin: origin, destination: destination, mode:"bicycling")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
let session = URLSession.shared
let url = URL(string:urlString)!
session.dataTask(with: url) { (data: Data?, response: URLResponse?, erorr: Error?) -> Void in
print("url: \(urlString)")
if let responseData = data {
do{
let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
因为这些点之间没有自行车道。 尝试select自行车:google maps
I tried to make a route by bike and car. It is impossible to build a bicycle route, and the route by car exists
Swift 3 个完整样本
import UIKit
class ViewController: UIViewController {
var origin = "55.930385, -3.118425"
var destination = "50.087692, 14.421150"
override func viewDidLoad() {
super.viewDidLoad()
durationDestance(origin: origin, destination: destination, mode:"bicycling")
durationDestance(origin: origin, destination: destination, mode:"auto")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)!
let session = URLSession.shared
let url = URL(string:urlString)!
session.dataTask(with: url) { (data: Data?, response: URLResponse?, erorr: Error?) -> Void in
print("url: \(urlString)")
if let responseData = data {
do{
let json = try JSONSerialization.jsonObject(with: responseData, options: JSONSerialization.ReadingOptions.allowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
}
Swift 2.3 全样本
import UIKit
class ViewController: UIViewController {
var origin = "55.930385, -3.118425"
var destination = "50.087692, 14.421150"
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
durationDestance(origin, destination: destination, mode:"bicycling")
durationDestance(origin, destination: destination, mode:"auto")
}
func durationDestance(origin: String, destination: String, mode:String) {
var urlString : String = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=\(origin)&destinations=\(destination)&mode=\(mode)&key=AIzaSyAVOt9LLagNGSOI8O0ri1Sbahcl_q5AFYc";
urlString = urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet())!
let session=NSURLSession.sharedSession();
let url=NSURL(string:urlString)!
session.dataTaskWithURL(url) { (data: NSData?,response: NSURLResponse?,erorr :NSError?) -> Void in
print("url: \(urlString)")
if let responseData=data{
do{
let json=try NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments);
print(json);
}
catch{
print("do not serialization :)");
}
}
}.resume();
}
}