我正在实施在谷歌地图中显示几个标记,直到现在它正在工作,我免除了Infowindows。
在我的项目中,我有: - AppDelegate.swift - ViewController.swift - CustomInfoWindow.xib(两个标签和一个按钮) - CustomInfoWindow.swift(标签和底部的IB)
详细说明:
ViewController.swift
import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
// Properties
var map:GMSMapView!
var longitudes:[Double]!
var latitudes:[Double]!
var architectNames:[String]!
var completedYear:[String]!
override func viewDidLoad() {
super.viewDidLoad()
//
latitudes = [48.8566667,41.8954656,51.5001524]
longitudes = [2.3509871,12.4823243,-0.1262362]
architectNames = ["Stephen Sauvestre","Bonanno Pisano","Augustus Pugin"]
completedYear = ["1889","1372","1859"]
//
self.map = GMSMapView(frame: self.view.frame)
self.view.addSubview(self.map)
self.map.delegate = self
// Add 3 markers
for i in 0...2 {
let coordinates = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
let marker = GMSMarker(position: coordinates)
marker.map = self.map
marker.icon = UIImage(named: "pin")
marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0.2)
marker.accessibilityLabel = "\(i)"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
class func instanceFromNib() -> CustomInfoWindow {
return UINib(nibName: "CustomInfoWindow", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomInfoWindow
}
var tappedMarker = GMSMarker()
//var infoWindow = CustomInfoWindow()
var infoWindow = CustomInfoWindow()
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
let index:Int! = Int(marker.accessibilityLabel!)
infoWindow = ViewController.instanceFromNib()
infoWindow.architectLbl.text = architectNames[index]
infoWindow.completedYearLbl.text = completedYear[index]
infoWindow.infoBtn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
self.view.addSubview(infoWindow)
return false
}
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
@objc func buttonTapped(sender: UIButton) {
print("Yeah! Button is tapped!")
}
}
CustomInfoWindow.swift
import UIKit
class CustomInfoWindow: UIView {
@IBOutlet var completedYearLbl: UILabel!
@IBOutlet var architectLbl: UILabel!
@IBOutlet weak var infoBtn: UIButton!
}
我遇到的问题是,当我点击市场时,当我预计它会出现在市场上并且我移动地图时,它会出现在屏幕上。
有关改进该细节代码的任何建议。
答案 0 :(得分:0)
您尚未定义:didTapAt
,功能:import UIKit
import GoogleMaps
class ViewController: UIViewController, GMSMapViewDelegate {
// Properties
var map:GMSMapView!
var longitudes:[Double]!
var latitudes:[Double]!
var architectNames:[String]!
var completedYear:[String]!
override func viewDidLoad() {
super.viewDidLoad()
//
latitudes = [48.8566667,41.8954656,51.5001524]
longitudes = [2.3509871,12.4823243,-0.1262362]
architectNames = ["Stephen Sauvestre","Bonanno Pisano","Augustus Pugin"]
completedYear = ["1889","1372","1859"]
//
self.map = GMSMapView(frame: self.view.frame)
self.view.addSubview(self.map)
self.map.delegate = self
// Add 3 markers
for i in 0...2 {
let coordinates = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
let marker = GMSMarker(position: coordinates)
marker.map = self.map
marker.icon = UIImage(named: "new-pin")
marker.accessibilityLabel = "\(i)"
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
class func instanceFromNib() -> CustomInfoWindow {
return UINib(nibName: "CustomInfoWindow", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomInfoWindow
}
var tappedMarker = GMSMarker()
var infoWindow = CustomInfoWindow()
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
let index:Int! = Int(marker.accessibilityLabel!)
print(index)
let location = CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude)
tappedMarker = marker
infoWindow.removeFromSuperview()
infoWindow = ViewController.instanceFromNib()
infoWindow.architectLbl.text = architectNames[index]
infoWindow.completedYearLbl.text = completedYear[index]
infoWindow.center = mapView.projection.point(for: location)
infoWindow.center.y = infoWindow.center.y - 107
infoWindow.infoBtn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
self.view.addSubview(infoWindow)
return false
}
func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
return UIView()
}
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
let location = CLLocationCoordinate2D(latitude: tappedMarker.position.latitude, longitude: tappedMarker.position.longitude)
infoWindow.center = mapView.projection.point(for: location)
infoWindow.center.y = infoWindow.center.y - 107
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) {
infoWindow.removeFromSuperview()
}
@objc func buttonTapped(sender: UIButton) {
print("Yeah! Button is tapped!")
}
}
和0 & 0 === 0
0 & 1 === 0
1 & 1 === 1
以下是更新后的代码:
1010
& 0101
= 0000
快乐的编码!