func didTapAt 坐标没有显示任何东西
the func didTapAt coordinate is not showing anything
我试图在用户通过 didTapAt 坐标函数点击地图后设置标记位置,但事件没有触发我不知道为什么
我尝试了不同的功能,比如在位置上点击 didtap,同样我没有错误,但也没有结果
import UIKit
import GoogleMaps
import Alamofire
class MapController: UIViewController {
var postArray = [AnyObject]()
var posts:NSArray = []
var bounds = GMSCoordinateBounds()
@IBOutlet var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
AddMarker(title: "test", snippet: "test" latitude: coordinate.latitude, longitude: coordinate.longitude)
}
func mapView(_ mapView: GMSMapView, didTapPOIWithPlaceID placeID: String,
name: String, location: CLLocationCoordinate2D) {
AddMarker(title: placeID, snippet: name, latitude: location.latitude, longitude: location.longitude)
print("You tapped \(name): \(placeID), \(location.latitude)/\(location.longitude)")
}
private func AddMarker(title:String , snippet:String , latitude:Double , longitude:Double){
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
marker.title = title
marker.snippet = snippet
marker.map = mapView
bounds = bounds.includingCoordinate(marker.position)
let update = GMSCameraUpdate.fit(bounds, withPadding: 100)
mapView.animate(with: update)
}
}
override func loadView() {
GMSServices.provideAPIKey("MyApiKey")
let camera = GMSCameraPosition.camera(withLatitude: 36, longitude: 10, zoom: 15)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
// AddMarker(title: "pala", snippet: "nanana", latitude: 30.89939467218524, longitude: 10.187976658344267)
}
}
mapView 需要对您的视图控制器的引用才能知道将事件发送到哪里。
您需要将 mapView.delegate
分配给 self
,并使您的 ViewController 符合适当的委托协议,例如:
class MapController: UIViewController, GMSMapViewDelegate
然后,在 loadView
中添加这一行。
mapView.delegate = self
我试图在用户通过 didTapAt 坐标函数点击地图后设置标记位置,但事件没有触发我不知道为什么
我尝试了不同的功能,比如在位置上点击 didtap,同样我没有错误,但也没有结果
import UIKit
import GoogleMaps
import Alamofire
class MapController: UIViewController {
var postArray = [AnyObject]()
var posts:NSArray = []
var bounds = GMSCoordinateBounds()
@IBOutlet var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
}
func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
AddMarker(title: "test", snippet: "test" latitude: coordinate.latitude, longitude: coordinate.longitude)
}
func mapView(_ mapView: GMSMapView, didTapPOIWithPlaceID placeID: String,
name: String, location: CLLocationCoordinate2D) {
AddMarker(title: placeID, snippet: name, latitude: location.latitude, longitude: location.longitude)
print("You tapped \(name): \(placeID), \(location.latitude)/\(location.longitude)")
}
private func AddMarker(title:String , snippet:String , latitude:Double , longitude:Double){
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
marker.title = title
marker.snippet = snippet
marker.map = mapView
bounds = bounds.includingCoordinate(marker.position)
let update = GMSCameraUpdate.fit(bounds, withPadding: 100)
mapView.animate(with: update)
}
}
override func loadView() {
GMSServices.provideAPIKey("MyApiKey")
let camera = GMSCameraPosition.camera(withLatitude: 36, longitude: 10, zoom: 15)
mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
// AddMarker(title: "pala", snippet: "nanana", latitude: 30.89939467218524, longitude: 10.187976658344267)
}
}
mapView 需要对您的视图控制器的引用才能知道将事件发送到哪里。
您需要将 mapView.delegate
分配给 self
,并使您的 ViewController 符合适当的委托协议,例如:
class MapController: UIViewController, GMSMapViewDelegate
然后,在 loadView
中添加这一行。
mapView.delegate = self