在 Google 地图中创建标记

Creating markers in GoogleMaps

如何创建一个函数,用于根据操作期间收到的一系列应用程序在地图上绘制标记。也就是说,函数必须在 viewDidLoad() 之外? 如果您使用具有以下内容的简单函数:

import UIKit
import GoogleMaps
import CoreLocation

class ViewController: UIViewController {

@IBOutlet var mMap: GMSMapView!

let locationManager = CLLocationManager()
let mapInsets = UIEdgeInsets(top: 10.0, left: 0.0, bottom: 100.0, right: 0.0)

override func viewDidLoad() {
        super.viewDidLoad()

    view = mMap
    mMap.padding = mapInsets 
    mMap.isMyLocationEnabled = true 
    mMap.settings.compassButton = true 
    mMap.settings.myLocationButton = true 
    mMap.settings.scrollGestures = true
    mMap.settings.zoomGestures = true

    let camera = GMSCameraPosition.camera(withLatitude: myLat, longitude: myLon, zoom: 15.0)
    let mMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera) 

    let buttonDps = UIButton(frame: CGRect(x: 2, y: 520, width: 103, height: 45))
    button.backgroundColor = .red
    button.setTitle("yes", for: .normal)
    button.titleLabel!.font = UIFont.boldSystemFont(ofSize: 19)
    button.layer.cornerRadius = 5.0   
    button.addTarget(self, action: #selector(buttonAct), for:.touchUpInside)
    self.view.addSubview(button)

}

func buttonAct(sender: UIButton!) {        
    let alert = UIAlertController(title:"help", message:"qwerty", preferredStyle:.alert)
    alert.addAction(UIAlertAction(title:"OK", style:.default){ action in
    self.markercreate()                        
    })
    alert.addAction(UIAlertAction(title:"cancel", style:.cancel, handler:nil))
    self.present(alert, animated:true, completion:nil)
    
}
        func markercreate(){
            let marker2 = GMSMarker()
            marker2.position = CLLocationCoordinate2D(latitude: 54.9044200,             longitude: 52.3154000)
            marker2.title = "Россия"
            marker2.snippet = "Москва"
            marker2.map = mMap
        }
}

然后什么也没有发生(((

据我分析,

代码可以正常工作。 marker 也添加到 map您只是看不到您添加的标记。将 mapcamera position 移动到 marker 位置,以便您可以看到标记,即

func markercreate()
{
    //Your code...
    mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) //Add this line to your code
}

我假设您将解决我在评论中提到的您创建的 2 mMap 个变量。

编辑:

1.Storyboard中,设置ViewController'sview'sclassGMSMapView并连接outlet mMap 到它,即

2. 按照下面代码段中的 comments:

override func viewDidLoad()
{
    super.viewDidLoad()

    //Your code...

    view = mMap //Remove this line
    mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) //Add this line
    let camera = GMSCameraPosition.camera(withLatitude: myLat, longitude: myLon, zoom: 15.0) //Remove this line
    let mMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera) //Remove this line

    //Your code...
}

假设你有一个纬度、经度和地名的列表。 如果你想显示标记然后使用它,请创建一个循环并在你的循环内。

func createMarker()
{
    let lon = Double(longResult as! String)

    let lat = Double(latResult as! String)

    print("Center_Name: \(centerName)")

    print("Longitude: \(longResult)")

    print("Latitude: \(latResult)")

    let markerResult = GMSMarker()

    markerResult.position = CLLocationCoordinate2D(latitude: lat!  , longitude: lon!)

    markerResult.title = "\(centerName)"

    markerResult.map = viewMap
}

我展示的代码是基本代码。有了这个,您可以在地图上创建一个标记。