GMSMapView(Google 地图)上的按钮使用 Swift
Button on the GMSMapView (Google Map) using Swift
由于我是 Swift 编程和 iOS 开发的新手,我面临将按钮放置在 Google 地图顶部的问题。
我一直在使用 Google Map SDK for iOS using Cocopods,示例来自 Google Map API 指南。
override func viewDidLoad() {
super.viewDidLoad()
var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true;
self.view = mapView
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
我想在地图上方放置按钮。
只需创建一个 UIButton
并在添加地图后将其添加到您的视图中,因为您 viewDidLoad
中的最后一件事应该没问题。
let button = UIButton(frame: whereYouWant)
self.view.addSubview(button)
假设您有一个名为 "googleMapView" 的 google 地图视图
和一个名为 "yourButton"
的 UIButton
确保通过执行以下操作将 GooglemapView 插入按钮下方:
self.view.insertSubview(googleMapView, atIndex: 0)
self.view.insertSubview(yourButton, atIndex: 1)
我尝试了几个选项但没有结果。最后发现在 view 上实现 GoogleMap 之后实现 button 或其他任何东西,就像这样:
override func viewDidLoad() {
super.viewDidLoad()
//Google Maps
// Create a GMSCameraPosition that tells the map to display the
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
// Add a button
let revealButton = UIButton()
let btnImage = UIImage(named: "reveal-icon")
revealButton.setImage(btnImage, for: .normal)
revealButton.frame = CGRect(x: 16, y: 38, width: 48, height: 41)
self.view.addSubview(revealButton)
// RevealViewController for button.
let revealViewController = self.revealViewController()
revealButton.addTarget(revealViewController,
action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
尝试在你的 ViewController class 中做这样的事情,也许在 viewDidLoad() 方法中:
假设self.view
是GmsMapView的一个实例,像这样,
self.view = GMSMapView.map(withFrame: .zero, camera: someCamera)
.
let btn: UIButton = UIButton(type: UIButtonType.roundedRect)
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("MyButton", for: UIControlState.normal)
self.view.addSubview(btn)
对于XCode 11
let btn: UIButton = UIButton(type: UIButton.ButtonType.roundedRect)
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("MyButton", for: UIControl.State.normal)
self.view.addSubview(btn)
这是将地图和物品放在上面的简单方法。
在 UIViewController 上创建您的按钮和您想要的一切
如果 任何 UIView 覆盖 ViewController 上的默认 UIView,请将它们的背景色设置为 Clear Color 然后在 viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 17.0)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
self.view.insertSubview(mapView, at: 0)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Main Title"
marker.snippet = "Deescription"
marker.map = mapView
self.okBtnOutlet.addTarget(self, action: #selector(placeMarkerPointOnDrag), for: .touchUpInside)
@objc func placeMarkerPointOnDrag(){
self.placeMarkerOnMapOnButtonTap(coordinate: self.draggingCoordinate)
}
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
print("End dragging")
let coordinate = mapView.myLocation!.coordinate
print(coordinate)
self.draggingCoordinate = marker.position
// self.placeMarkerOnMapOnButtonTap(coordinate: marker.position)
}
func placeMarkerOnMapOnButtonTap(coordinate : CLLocationCoordinate2D){
let geocoder = GMSGeocoder()
geocoder.reverseGeocodeCoordinate(coordinate) {
response, error in
//
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
}
else {
if let places = response?.results() {
if let place = places.first {
if let lines = place.lines {
print("GEOCODE: Formatted Address: \(lines)")
let temp = CoordinatesValue(lineString: lines.first ?? "", title: "", lat: coordinate.latitude, long: coordinate.longitude, isSelect: false, isCompleted: false)
self.locArray.append(temp)
}
if let newPlace = place.postalCode{
print(newPlace)
}
}
else {
print("GEOCODE: nil first in places")
}
}
else {
print("GEOCODE: nil in places")
}
}
}
let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
let marker = GMSMarker(position: position)
marker.title = ""
marker.map = self.googleMapView
self.selectedCoordinate = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
// let distanceInMeters : CLLocationDistance = self.currentCoordinate.distance(from: selectedCoordinate)
// print(distanceInMeters)
print("The CURRENT LAT \(lat) & LONG \(long)")
}
由于我是 Swift 编程和 iOS 开发的新手,我面临将按钮放置在 Google 地图顶部的问题。
我一直在使用 Google Map SDK for iOS using Cocopods,示例来自 Google Map API 指南。
override func viewDidLoad() {
super.viewDidLoad()
var camera = GMSCameraPosition.cameraWithLatitude(-33.86, longitude: 151.20, zoom: 6)
var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true;
self.view = mapView
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
}
我想在地图上方放置按钮。
只需创建一个 UIButton
并在添加地图后将其添加到您的视图中,因为您 viewDidLoad
中的最后一件事应该没问题。
let button = UIButton(frame: whereYouWant)
self.view.addSubview(button)
假设您有一个名为 "googleMapView" 的 google 地图视图 和一个名为 "yourButton"
的 UIButton确保通过执行以下操作将 GooglemapView 插入按钮下方:
self.view.insertSubview(googleMapView, atIndex: 0)
self.view.insertSubview(yourButton, atIndex: 1)
我尝试了几个选项但没有结果。最后发现在 view 上实现 GoogleMap 之后实现 button 或其他任何东西,就像这样:
override func viewDidLoad() {
super.viewDidLoad()
//Google Maps
// Create a GMSCameraPosition that tells the map to display the
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 6.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
// Creates a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: -33.86, longitude: 151.20)
marker.title = "Sydney"
marker.snippet = "Australia"
marker.map = mapView
// Add a button
let revealButton = UIButton()
let btnImage = UIImage(named: "reveal-icon")
revealButton.setImage(btnImage, for: .normal)
revealButton.frame = CGRect(x: 16, y: 38, width: 48, height: 41)
self.view.addSubview(revealButton)
// RevealViewController for button.
let revealViewController = self.revealViewController()
revealButton.addTarget(revealViewController,
action: #selector(SWRevealViewController.revealToggle(_:)), for: .touchUpInside)
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
尝试在你的 ViewController class 中做这样的事情,也许在 viewDidLoad() 方法中:
假设self.view
是GmsMapView的一个实例,像这样,
self.view = GMSMapView.map(withFrame: .zero, camera: someCamera)
.
let btn: UIButton = UIButton(type: UIButtonType.roundedRect)
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("MyButton", for: UIControlState.normal)
self.view.addSubview(btn)
对于XCode 11
let btn: UIButton = UIButton(type: UIButton.ButtonType.roundedRect)
btn.frame = CGRect(x: 100, y: 100, width: 100, height: 30)
btn.setTitle("MyButton", for: UIControl.State.normal)
self.view.addSubview(btn)
这是将地图和物品放在上面的简单方法。 在 UIViewController 上创建您的按钮和您想要的一切 如果 任何 UIView 覆盖 ViewController 上的默认 UIView,请将它们的背景色设置为 Clear Color 然后在 viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 17.0)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera: camera)
mapView.isMyLocationEnabled = true
self.view.insertSubview(mapView, at: 0)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20)
marker.title = "Main Title"
marker.snippet = "Deescription"
marker.map = mapView
self.okBtnOutlet.addTarget(self, action: #selector(placeMarkerPointOnDrag), for: .touchUpInside)
@objc func placeMarkerPointOnDrag(){
self.placeMarkerOnMapOnButtonTap(coordinate: self.draggingCoordinate)
}
func mapView(_ mapView: GMSMapView, didEndDragging marker: GMSMarker) {
print("End dragging")
let coordinate = mapView.myLocation!.coordinate
print(coordinate)
self.draggingCoordinate = marker.position
// self.placeMarkerOnMapOnButtonTap(coordinate: marker.position)
}
func placeMarkerOnMapOnButtonTap(coordinate : CLLocationCoordinate2D){
let geocoder = GMSGeocoder()
geocoder.reverseGeocodeCoordinate(coordinate) {
response, error in
//
if error != nil {
print("reverse geodcode fail: \(error!.localizedDescription)")
}
else {
if let places = response?.results() {
if let place = places.first {
if let lines = place.lines {
print("GEOCODE: Formatted Address: \(lines)")
let temp = CoordinatesValue(lineString: lines.first ?? "", title: "", lat: coordinate.latitude, long: coordinate.longitude, isSelect: false, isCompleted: false)
self.locArray.append(temp)
}
if let newPlace = place.postalCode{
print(newPlace)
}
}
else {
print("GEOCODE: nil first in places")
}
}
else {
print("GEOCODE: nil in places")
}
}
}
let position = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude)
let marker = GMSMarker(position: position)
marker.title = ""
marker.map = self.googleMapView
self.selectedCoordinate = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)
// let distanceInMeters : CLLocationDistance = self.currentCoordinate.distance(from: selectedCoordinate)
// print(distanceInMeters)
print("The CURRENT LAT \(lat) & LONG \(long)")
}