gpt4 book ai didi

ios - 在按钮函数中添加两个坐标以启动 mapKit 并开始两点之间的导航(Swift)

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

我正在使用这个类

import UIKit
import CoreLocation
import GoogleMaps
import GooglePlaces
import SwiftyJSON
import Alamofire
import MapKit

class FinalClass: UIViewController {

@IBOutlet weak var containerView: UIView!
@IBOutlet weak var bottomInfoView: UIView!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var distanceLabel: UILabel!



var userLocation:CLLocationCoordinate2D?
var places:[QPlace] = []
var index:Int = -1

var locationStart = CLLocation()
var locationEnd = CLLocation()

var mapView:GMSMapView!
var marker:GMSMarker?






override func loadView() {
super.loadView()
}

override func viewDidLoad() {
super.viewDidLoad()



guard index >= 0, places.count > 0 else {
return


}

let place = places[index]
let lat = place.location?.latitude ?? 1.310844
let lng = place.location?.longitude ?? 103.866048

// Google map view
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 12.5)
mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.autoresizingMask = [.flexibleHeight, .flexibleWidth, .flexibleTopMargin, .flexibleBottomMargin, .flexibleLeftMargin, .flexibleRightMargin]
self.containerView.addSubview(mapView)

// Add gesture
addSwipeGesture()

didSelect(place: place)
if userLocation != nil {
addMarkerAtCurrentLocation(userLocation!)

}
}

func addSwipeGesture() {
let directions: [UISwipeGestureRecognizerDirection] = [.right, .left]
for direction in directions {
let gesture = UISwipeGestureRecognizer(target: self, action: #selector(handleSwipe(sender:)))
gesture.direction = direction
self.bottomInfoView.addGestureRecognizer(gesture)
}
}

func addMarkerAtCurrentLocation(_ userLocation: CLLocationCoordinate2D) {
let marker = GMSMarker()
marker.position = userLocation
marker.title = "Your location"
marker.map = mapView
}

func didSelect(place:QPlace) {

guard let coordinates = place.location else {
return
}

// clear current marker
marker?.map = nil

// add marker
marker = GMSMarker()
marker?.position = coordinates
marker?.title = place.name
marker?.map = mapView
mapView.selectedMarker = marker
moveToMarker(marker!)

// update bottom info panel view
let desc = place.getDescription()
descriptionLabel.text = desc.characters.count > 0 ? desc : "-"
distanceLabel.text = "-"

// update distance
if userLocation != nil {
let dist = distance(from: userLocation!, to: coordinates)
distanceLabel.text = String.init(format: "Distance %.2f meters", dist)
self.drawPath(startLocation: userLocation!, endLocation: coordinates)
}

title = place.name
}

func moveToMarker(_ marker: GMSMarker) {
let camera = GMSCameraPosition.camera(withLatitude: marker.position.latitude,
longitude: marker.position.longitude,
zoom: 12.5)
self.mapView.animate(to: camera)
}

// distance between two coordinates
func distance(from: CLLocationCoordinate2D, to: CLLocationCoordinate2D) -> CLLocationDistance {
let from = CLLocation(latitude: from.latitude, longitude: from.longitude)
let to = CLLocation(latitude: to.latitude, longitude: to.longitude)

return from.distance(from: to)

}

func handleSwipe(sender: UISwipeGestureRecognizer) {

guard index >= 0, places.count > 0 else {
return
}

if sender.direction == .left {
if index < places.count - 2 {
index += 1
didSelect(place: places[index])
}
} else if sender.direction == .right {
if index > 1 {
index -= 1
didSelect(place: places[index])
}
}
}




func drawPath(startLocation: CLLocationCoordinate2D, endLocation: CLLocationCoordinate2D) {

let from = CLLocation(latitude: startLocation.latitude, longitude: startLocation.longitude)
let to = CLLocation(latitude: endLocation.latitude, longitude: endLocation.longitude)

let origin = "\(from.coordinate.latitude),\(from.coordinate.longitude)"
let destination = "\(to.coordinate.latitude),\(to.coordinate.longitude)"


let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

Alamofire.request(url).responseJSON { response in

print(response.request as Any) // original URL request
print(response.response as Any) // HTTP URL response
print(response.data as Any) // server data
print(response.result as Any) // result of response serialization

let json = JSON(data: response.data!)
let routes = json["routes"].arrayValue

// print route using Polyline
for route in routes
{
let routeOverviewPolyline = route["overview_polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
let path = GMSPath.init(fromEncodedPath: points!)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.black
polyline.map = self.mapView
}

}

}


@IBAction func navigationStart(_ sender: Any) {





}

用谷歌地图添加地点标记,在 map 上绘制方向并显示两点之间的距离,现在我想在 startLocation: userLocation! 之间启动导航器endLocation: coordinates 但经过一些研究,我发现我无法在同一 View 中启动导航器,我需要打开 map 应用程序,所以我决定添加 MapKit 和一个按钮

@IBAction func navigationStart(_ sender: Any) {


}

那么我如何通过按下 map 应用程序以从 userLocationcoordinates 的方向打开的按钮来做到这一点?我已经看过类似的问题,但与我的问题有点不同,因为我已经有了要点,但格式不同。

最佳答案

你的问题有点令人困惑,但如果你想打开 map 应用程序并显示从用户当前位置到 map 上另一点的方向,那么你不需要传递用户的位置,只需传递目的地:

swift 4:

let coordinate = CLLocationCoordinate2DMake(51.5007, -0.1246)
let placeMark = MKPlacemark(coordinate: coordinate)
let mapItem = MKMapItem(placemark: placeMark)
mapItem.name = "Big Ben"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])

objective-c :

CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(51.5007, -0.1246);
MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate: coordinate];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placemark];
[mapItem setName:@"Big Ben"];
[mapItem openInMapsWithLaunchOptions:@{MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving}];

关于ios - 在按钮函数中添加两个坐标以启动 mapKit 并开始两点之间的导航(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47095622/

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