我是Swift的新手并完全编码。
到目前为止,我已经显示了自定义信息窗口。但是,我在将数据填充到其中时遇到了问题。我想我在这里遗漏了一些东西,然而,我无法理解它的错误。
我已经使用断点检查数据是否正确填充,但是它没有在地图上更新。我得到了我输入的虚拟文本。
import UIKit
import GoogleMaps
class LocationsViewController: UIViewController, CLLocationManagerDelegate, GMSMapViewDelegate {
let locationManager = CLLocationManager()
var userLocation: CLLocation?
var branch: Branch?
var tappedMarker : GMSMarker?
var customInfoWindow : CustomInfoWindow?
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: GMSCameraPosition.camera(withLatitude: 25.1972948, longitude: 55.3266862, zoom: 10.0))
var mapViewContainer: UIView = {
let view = UIView()
view.backgroundColor = .red
return view
}()
let getDirectionsButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Get Directions", for: .normal)
button.addTarget(self, action: #selector(getDirections), for: .touchUpInside)
return button
}()
func getDirections() {
if userLocation != nil {
guard let userLocationLatitude = userLocation?.coordinate.latitude else { return }
guard let userLocationLongitude = userLocation?.coordinate.longitude else { return }
let directionsUrl = "https://www.google.com/maps/dir/?api=1&origin=\(userLocationLatitude),\(userLocationLongitude)&destination=25.1855878,55.2407415"
let encodedUrl = directionsUrl.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed)
guard let url = URL(string: encodedUrl!) else { return }
let alertController = UIAlertController(title: "Get Directions using:", message: nil, preferredStyle: .actionSheet)
alertController.addAction(UIAlertAction(title: "Google Maps", style: .default, handler: { (_) in
UIApplication.shared.open(url, options:[:], completionHandler: { (_) in })
}))
alertController.addAction(UIAlertAction(title: "Maps", style: .default, handler: nil))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: nil)
}
}
override func loadView() {
super.loadView()
view.backgroundColor = .white
mapViewContainer = mapView
self.mapView.delegate = self
view.addSubview(mapViewContainer)
mapViewContainer.anchor(top: topLayoutGuide.bottomAnchor, left: view.leftAnchor, bottom: bottomLayoutGuide.topAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
view.addSubview(getDirectionsButton)
getDirectionsButton.anchor(top: nil, left: nil, bottom: bottomLayoutGuide.topAnchor, right: view.rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 50, paddingRight: 20, width: 0, height: 0)
// Creates a marker in the center of the map.
//25.1855878,55.2407415
let alsafaMarker = GMSMarker()
alsafaMarker.position = CLLocationCoordinate2D(latitude: 25.1855878, longitude: 55.2407415)
alsafaMarker.title = "Al Safa Park Branch"
alsafaMarker.snippet = "112 Al Hadiqa Rd \nT: 043218838 \nSun to Sat | 11:00am - 2:00am"
alsafaMarker.icon = UIImage(named: "marker")
alsafaMarker.appearAnimation = .pop
alsafaMarker.map = mapView
//25.2347973,55.4743648
let lastExitMarker = GMSMarker()
lastExitMarker.position = CLLocationCoordinate2D(latitude: 25.2347973, longitude: 55.4743648)
lastExitMarker.title = "Last Exit Branch"
lastExitMarker.snippet = "Last Exit Al Khawaneej - D89\n T: 0544422138 \nOpen 24 Hours"
lastExitMarker.icon = UIImage(named: "marker")
lastExitMarker.appearAnimation = .pop
lastExitMarker.map = mapView
}
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.titleView = UIImageView(image: #imageLiteral(resourceName: "mooyahLabelLogo"))
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
locationManager.startUpdatingLocation()
self.tappedMarker = GMSMarker()
self.customInfoWindow = CustomInfoWindow().loadView()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
userLocation = location
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
print("marker was tapped")
tappedMarker = marker
if let tappedMarkerTitle = tappedMarker?.title {
if let tappedMarkerSnippet = tappedMarker?.snippet {
branch = Branch(name: tappedMarkerTitle, snippet: tappedMarkerSnippet)
}
}
// get location of the tapped marker
let position = marker.position
mapView.animate(toLocation: position)
let point = mapView.projection.point(for: position)
let newPoint = mapView.projection.coordinate(for: point)
let camera = GMSCameraUpdate.setTarget(newPoint)
mapView.animate(with: camera)
let opaqueWhite = UIColor(white: 1, alpha: 0.9)
customInfoWindow?.layer.backgroundColor = opaqueWhite.cgColor
customInfoWindow?.layer.cornerRadius = 8
customInfoWindow?.center = mapView.projection.point(for: position)
self.mapView.addSubview(customInfoWindow!)
return false
}
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
let view = CustomInfoWindow()
view.branchNameLabel.text = branch?.name
view.branchSnippetLabel.text = branch?.snippet
return view
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
customInfoWindow?.removeFromSuperview()
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let position = tappedMarker?.position
customInfoWindow?.center = mapView.projection.point(for: position!)
customInfoWindow?.center.y -= 130
}
}
import UIKit
import GoogleMaps
class CustomInfoWindow: UIView {
let branchNameLabel: UILabel = {
let label = UILabel()
label.text = "Branch Name"
label.font = UIFont.boldSystemFont(ofSize: 16)
return label
}()
let branchSnippetLabel: UILabel = {
let label = UILabel()
label.text = "This is where the full address will be. This is a very long address \nT: 043218838 \nSun to Sat | 11:00am - 2:00am"
label.font = UIFont.boldSystemFont(ofSize: 14)
label.numberOfLines = 4
label.textColor = UIColor.gray
return label
}()
let getDirectionsButton: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Get Directions", for: .normal)
button.addTarget(self, action: #selector(handleDirections), for: .touchUpInside)
return button
}()
func handleDirections() {
print("Get Directions button tapped")
}
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func loadView() -> CustomInfoWindow{
let customInfoWindow = Bundle.main.loadNibNamed("CustomInfoWindow", owner: self, options: nil)?[0] as! CustomInfoWindow
customInfoWindow.addSubview(branchNameLabel)
branchNameLabel.anchor(top: customInfoWindow.topAnchor, left: customInfoWindow.leftAnchor, bottom: nil, right: nil, paddingTop: 15, paddingLeft: 15, paddingBottom: 0, paddingRight: 0, width: 0, height: 0)
customInfoWindow.addSubview(branchSnippetLabel)
branchSnippetLabel.anchor(top: branchNameLabel.bottomAnchor, left: branchNameLabel.leftAnchor, bottom: nil, right: customInfoWindow.rightAnchor, paddingTop: 5, paddingLeft: 0, paddingBottom: 0, paddingRight: 15, width: 0, height: 0)
customInfoWindow.addSubview(getDirectionsButton)
getDirectionsButton.anchor(top: nil, left: nil, bottom: customInfoWindow.bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 15, paddingRight: 0, width: 0, height: 0)
getDirectionsButton.centerXAnchor.constraint(equalTo: customInfoWindow.centerXAnchor).isActive = true
return customInfoWindow
}
}
import UIKit
class Branch {
var name: String?
var snippet: String?
init(name: String, snippet: String) {
self.name = name
self.snippet = snippet
}
}