gpt4 book ai didi

ios - 有时会返回 Google 距离矩阵 ZERO_RESULTS

转载 作者:行者123 更新时间:2023-11-28 16:00:29 26 4
gpt4 key购买 nike

my first question如果我设置默认的 latlong 并且现在我尝试获取位置当前位置并使用它,那么它是有效的。我也关注这个Get User's Current Location / Coordinates但未能达成结果。

我不确定这里发生了什么它不起作用尝试以多种方式进行

   func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

//print(locations)
//get the actual location from the device and first one each move
let userClLocation : CLLocation = locations[0]

let latitude = userClLocation.coordinate.latitude
let longitude = userClLocation.coordinate.longitude


durationDestance(origin: "\(latitude),\(longitude)", 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();
}

API每次json响应

status = "ZERO_RESULTS";

我的目标:我不确定为什么代码不起作用我的主要目标是:获取用户当前位置和目的地以计算持续时间和公里数。

最佳答案

详情

swift 3、xCode 8.2

获取当前位置的完整示例

ViewController.swift

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true

}

private func updateCurrentLocation() {
locationManager.startUpdatingLocation()
}

@IBAction func showCurrentLocation(_ sender: UIBarButtonItem) {
updateCurrentLocation()
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0] as CLLocation

manager.stopUpdatingLocation()

let coordinations = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude,longitude: userLocation.coordinate.longitude)
let span = MKCoordinateSpanMake(0.2,0.2)
let region = MKCoordinateRegion(center: coordinations, span: span)

mapView.setRegion(region, animated: true)
print("Coordinate \(userLocation.coordinate)")

}
}

Mains.storyboard

 <?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="u20-DK-FBK">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="stackoverflow_25296691" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<mapView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" mapType="standard" translatesAutoresizingMaskIntoConstraints="NO" id="5RZ-FN-eIO">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
</mapView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="5RZ-FN-eIO" firstAttribute="leading" secondItem="8bC-Xf-vdC" secondAttribute="leading" id="BDg-DH-xgo"/>
<constraint firstAttribute="trailing" secondItem="5RZ-FN-eIO" secondAttribute="trailing" id="Bhl-4P-9cd"/>
<constraint firstItem="5RZ-FN-eIO" firstAttribute="bottom" secondItem="wfy-db-euE" secondAttribute="top" id="W2W-nx-2X6"/>
<constraint firstItem="5RZ-FN-eIO" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="fuU-IJ-q2W"/>
</constraints>
</view>
<navigationItem key="navigationItem" id="qbN-90-njV">
<barButtonItem key="rightBarButtonItem" title="Location" id="hoa-eP-AC4">
<connections>
<action selector="showCurrentLocation:" destination="BYZ-38-t0r" id="Wh7-el-edQ"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="mapView" destination="5RZ-FN-eIO" id="mL4-gS-1Qz"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="972" y="37.331334332833585"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="rry-ls-hJW">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="u20-DK-FBK" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" id="bcA-NR-zma">
<rect key="frame" x="0.0" y="0.0" width="375" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="rootViewController" id="xyN-dx-lCn"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="tlB-hp-Doq" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="32.799999999999997" y="37.331334332833585"/>
</scene>
</scenes>
</document>

Info.plist

添加

<key>NSLocationWhenInUseUsageDescription</key>
<string>Location Test</string>

模拟器设置

enter image description here

结果

enter image description here enter image description here

关于ios - 有时会返回 Google 距离矩阵 ZERO_RESULTS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41155676/

26 4 0
文章推荐: arrays - 如何在 Swift 3 中向结构数组添加新变量?
文章推荐: css:在中间垂直对齐文本
文章推荐: jquery - 如何使用 Jquery 在点击时将

添加到

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com