gpt4 book ai didi

Swift iOS 谷歌地图,坐标路径

转载 作者:搜寻专家 更新时间:2023-10-31 21:49:44 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中创建一个功能,它将引导用户找到我创建的标记。这是我正在使用的代码,效果很好,它获取用户当前位置并将其显示在 map 上。但是我怎样才能得到一个标记的方向呢?

任何awnser都会有帮助

class Karta: UIViewController, CLLocationManagerDelegate {
@IBOutlet var mapView: GMSMapView!

let locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

//allow app to track user
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()

//set out a marker on the map
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(56.675907, 12.858798)
marker.appearAnimation = kGMSMarkerAnimationPop
marker.icon = UIImage(named: "flag_icon")
marker.map = mapView
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "Types Segue" {
let navigationController = segue.destinationViewController as UINavigationController
}
}

func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

//If map is being used
if status == .AuthorizedWhenInUse {
var myLocation = mapView
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if let location = locations.first as? CLLocation {
mapView.camera = GMSCameraPosition(target: location.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
locationManager.stopUpdatingLocation()
}
}
}

最佳答案

所以我最近刚刚解决了这个问题,这是我使用最新版本的 Alamofire (4.3) 实现的 Swift 3

 func fetchMapData() { 

let directionURL = "https://maps.googleapis.com/maps/api/directions/json?" +
"origin=\(originAddressLat),\(originAddressLng)&destination=\(destinationAddressLat),\(destinationAddressLong)&" +
"key=YOUROWNSERVERKEY"



Alamofire.request(directionURL).responseJSON
{ response in

if let JSON = response.result.value {

let mapResponse: [String: AnyObject] = JSON as! [String : AnyObject]

let routesArray = (mapResponse["routes"] as? Array) ?? []

let routes = (routesArray.first as? Dictionary<String, AnyObject>) ?? [:]

let overviewPolyline = (routes["overview_polyline"] as? Dictionary<String,AnyObject>) ?? [:]
let polypoints = (overviewPolyline["points"] as? String) ?? ""
let line = polypoints

self.addPolyLine(encodedString: line)
}
}

}

func addPolyLine(encodedString: String) {

let path = GMSMutablePath(fromEncodedPath: encodedString)
let polyline = GMSPolyline(path: path)
polyline.strokeWidth = 5
polyline.strokeColor = .blue
polyline.map = whateverYourMapViewObjectIsCalled

}

关于Swift iOS 谷歌地图,坐标路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28784034/

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