我尝试在两个位置之间画线,
并且成功显示了origin
的标记和destination
的标记,
但是该行未出现在origin
和destination
ScreenShoot之间
我的代码:
import UIKit
import GoogleMaps
import GooglePlaces
import Alamofire
import SwiftyJSON
import HCSStarRatingView
class CategoryViewController: UIViewController , UITableViewDataSource
, GMSMapViewDelegate {
@IBOutlet weak var gms: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 31.529679,
longitude: 34.459619,
zoom: 13,
bearing: 30,
viewingAngle: 40)
//Setting the googleView
self.gms.camera = camera
self.gms.delegate = self
self.gms?.isMyLocationEnabled = true
self.gms.settings.myLocationButton = true
self.gms.settings.compassButton = true
self.gms.settings.zoomGestures = true
self.gms.animate(to: camera)
//Setting the start and end location
let origin = "\(31.529679),\(34.459619)"
let destination = "\(31.5201027),\(34.4480504)"
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"
//Rrequesting Alamofire and SwiftyJSON
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) // result of response serialization
let json = try! JSON(data: response.data!)
let routes = json["routes"].arrayValue
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.strokeColor = UIColor.red
polyline.strokeWidth = 10
polyline.map = self.gms
}
}
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 31.529679,
longitude: 34.459619)
marker.title = "Mobiloitte"
marker.snippet = "India"
marker.map = gms
//28.643091, 77.218280
let marker1 = GMSMarker()
marker1.position = CLLocationCoordinate2D(latitude: 31.5201027,
longitude: 34.4480504)
marker1.title = "NewDelhi"
marker1.snippet = "India"
marker1.map = gms
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}