gpt4 book ai didi

json - 使用按钮更新用户位置并快速绘制路线

转载 作者:行者123 更新时间:2023-11-28 16:13:47 25 4
gpt4 key购买 nike

我有一个在用户位置和标记之间绘制路线的应用程序。它工作正常,但如果用户更改其位置并插入其他标记,则路线会从第一个位置绘制。

这是合乎逻辑的。 locationManager 函数正在停止更新位置以节省电量。这是代码:

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

userLocation = locations[0]
long = userLocation.coordinate.longitude;
lat = userLocation.coordinate.latitude;
locationManager.stopUpdatingLocation()

if let location = locations.first {

mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 14, bearing: 0, viewingAngle: 0)
}
}

然后我创建一个按钮来更新用户位置并重新绘制折线:

@IBAction func ActualizarLocalizacion(sender: AnyObject) {
locationManager.startUpdatingLocation()

originAddresslong = "\(userLocation.coordinate.longitude)"
originAddresslat = "\(userLocation.coordinate.latitude)"

if markerLocation == nil
{markerLocation = userLocation.coordinate
}

destinationAddresslong = "\(markerLocation.longitude)"
destinationAddresslat = "\(markerLocation.latitude)"


var directionsURLString = baseURLDirections + "origin=" + originAddresslat + "," + originAddresslong + "&destination=" + destinationAddresslat + "," + destinationAddresslong + "&key=MyKey"
directionsURLString = directionsURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let directionsURL = NSURL(string: directionsURLString)


Alamofire.request(.GET, directionsURL!, parameters: nil).responseJSON { response in

switch response.result {

case .Success(let data):

var json = JSON(data)
print(json)

let errornum = json["error"]


if (errornum == true){



}else{

//NSThread.sleepForTimeInterval (2)

var routes = json["routes"].array

if routes != nil{

var overViewPolyLine = routes![0]["overview_polyline"]["points"].string
print(overViewPolyLine)
if overViewPolyLine != nil{

if self.routePolyline != nil {
self.routePolyline.map = nil
self.routePolyline = nil
}


let path = GMSMutablePath(fromEncodedPath: overViewPolyLine)
self.routePolyline = GMSPolyline(path: path)
self.routePolyline.strokeWidth = 5
self.routePolyline.strokeColor = UIColor.blueColor()
self.routePolyline.map = self.mapView
overViewPolyLine = nil
routes = nil
json = nil

}

}
}
case .Failure(let error):

print("Hubo un problema con el servidor de direcciones: \(error)")
}

}
}

然后我看到我必须按下按钮 3 次 (!!!) 才能正确重绘。我用过代码

   locationManager.startUpdatingLocation()

NSThread.sleepForTimeInterval (2)

然后我只需要按 2 次,但我不知道为什么会这样。我怀疑这一定是处理时间的问题,但我不知道如何处理。

我在用户点击标记时使用的功能是相同的(公式和变量),在这种情况下它只需要按一次。

谢谢大家。

PD:

这些是我的进口:

import UIKit
import GoogleMaps
import SRKUtility
import SRKRequestManager
import Alamofire
import SwiftyJSON

这是我的 markerLocation 变量:

func mapView(mapView: GMSMapView!, didTapMarker marker: GMSMarker!) -> Bool {

markerLocation = marker.position;
}

最佳答案

我明白了。最后我把按钮和重绘代码分开了,这在locationmanager中已经介绍过了。这是最终代码:

1-位置管理器 func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    userLocation = locations[0]
long = userLocation.coordinate.longitude;
lat = userLocation.coordinate.latitude;

originAddresslong = "\(userLocation.coordinate.longitude)"
originAddresslat = "\(userLocation.coordinate.latitude)"

if markerLocation == nil
{markerLocation = userLocation.coordinate
}

destinationAddresslong = "\(markerLocation.longitude)"
destinationAddresslat = "\(markerLocation.latitude)"



var directionsURLString = baseURLDirections + "origin=" + originAddresslat + "," + originAddresslong + "&destination=" + destinationAddresslat + "," + destinationAddresslong + "&key=AIzaSyB4xO_8B0ZoA8lsAgRjqpqJjgWHbb5X3u0"
directionsURLString = directionsURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let directionsURL = NSURL(string: directionsURLString)


Alamofire.request(.GET, directionsURL!, parameters: nil).responseJSON { response in

switch response.result {

case .Success(let data):

var json = JSON(data)
print(json)

let errornum = json["error"]


if (errornum == true){



}else{

//NSThread.sleepForTimeInterval (2)

var routes = json["routes"].array

if routes != nil{

var overViewPolyLine = routes![0]["overview_polyline"]["points"].string
let distancia = routes![0]["legs"][0]["distance"]["text"].string
if overViewPolyLine != nil{

if self.routePolyline != nil {
self.routePolyline.map = nil
self.routePolyline = nil
}


let path = GMSMutablePath(fromEncodedPath: overViewPolyLine)
self.routePolyline = GMSPolyline(path: path)
self.routePolyline.strokeWidth = 5
self.routePolyline.strokeColor = UIColor.blueColor()
self.routePolyline.map = self.mapView

self.DistanciaLabel.setTitle(distancia,forState: UIControlState.Normal)

overViewPolyLine = nil
routes = nil
json = nil

}

}
}
case .Failure(let error):

print("Hubo un problema con el servidor de direcciones: \(error)")
}

}

locationManager.stopUpdatingLocation()

if let location = locations.first {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 14, bearing: 0, viewingAngle: 0)

}

}

2- 按钮

    @IBAction func ActualizarLocalizacion(sender: AnyObject) {
locationManager.startUpdatingLocation()
}

关于json - 使用按钮更新用户位置并快速绘制路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305316/

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