ios - 在 Google map 中创建标记

标签 ios swift xcode google-maps google-maps-markers

如何创建一个函数,用于根据操作期间收到的一系列应用程序在 map 上绘制标记。也就是说,函数必须在 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
        }
}

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

最佳答案

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

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
}

我展示的代码是基本代码。有了它,您可以在 map 上创建标记。

关于ios - 在 Google map 中创建标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45784747/

相关文章:

ios - 如何将变量分配给按钮或从按钮调用变量

ios - 应用在待审核阶段可以修改应用截图吗?

ios - 通过 WhatsApp 共享 PDF

ios - 如何使空的 UITextView 像 iOS 上的 Notes 一样可滚动?

ios - UIBarButtonItem 在 iPhone 而不是 iPad 上显示图像

objective-c - 当 UIProgressView 已满时加载新 View

ios - Xcode 控制台中我的应用程序名称旁边括号中的数字是什么意思?

c++ - xcode 包含路径冲突

swift - 使用 Swift 格式化日期

ios - UISlider 的 minView 超出了它的边界和拇指的中心