在GoogleMaps中创建标记

时间:2017-08-20 17:06:51

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

如何创建一个函数,用于在操作期间从应用程序接收的应用程序数组中绘制标记。也就是说,函数必须在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
        }
}




然后没有任何反应((

2 个答案:

答案 0 :(得分:1)

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

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
}

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

答案 1 :(得分:0)

据我所知,

代码可以正常工作。此外,marker已添加到map您只是看不到添加的标记。将camera position的{​​{1}}移至map位置,以便您可以看到标记,即

marker

我假设您将解析我在评论中提到的2 func markercreate() { //Your code... mMap.camera = GMSCameraPosition.camera(withLatitude: 54.9044200, longitude: 52.3154000, zoom: 15.0) //Add this line to your code } 个变量。

修改

1。mMap中,将Storyboard ViewController's view's设置为class并连接GMSMapView outlet到它,即

enter image description here

2。请按照以下代码段中的mMap进行操作:

comments