为什么我的Google地图应用不会打印出敲击坐标?

时间:2017-03-30 21:33:26

标签: ios swift google-maps

我有以下代码,如果我编译它不起作用:如果我更改缩放值,并再次重新编译它就像“没有改变”。此外,当我点击时,打印功能不会打印任何内容。可能是什么问题?

import UIKit
import GoogleMaps
import MapKit

class ViewController: UIViewController, GMSMapViewDelegate, UIAlertViewDelegate {
@IBOutlet weak var mapView: GMSMapView!
var markersArray: Array<CLLocationCoordinate2D> = []

override func viewDidLoad() {
//set default position of the mapView
let camera = GMSCameraPosition.camera(withLatitude: 1.285, longitude: 10.848, zoom: 10)
let mapView = GMSMapView.map(withFrame: self.view.bounds, camera:camera)
//mapView.mapType = kGMSTypeSatellite - use of unresolved identifier 'kGMSTypeSatellite'
mapView.isMyLocationEnabled = true
mapView.settings.compassButton = true
mapView.settings.myLocationButton = true
mapView.camera = camera
mapView.delegate = self
}

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D){
var lat : String = coordinate.latitude.description
var lng : String = coordinate.longitude.description
markersArray.append(coordinate)
print(markersArray)
print(lat)
print(lng)
    print(coordinate.latitude)
    print(coordinate.longitude)
}

1 个答案:

答案 0 :(得分:1)

我相信您的问题是您要在线上创建第二个地图视图:

let mapView = GMSMapView.map(withFrame: self.view.bounds, camera:camera)

并将其委托设置为自己。您永远不会将此地图添加到视图中。您看到并触摸的地图视图来自您的故事板,您有一个出口:

@IBOutlet weak var mapView: GMSMapView!

哪个不一样。您创建具有相同名称的本地变量。你永远不会设置这个出口的代表。只需设置插座的摄像机位置,而不是创建新的地图视图:

override func viewDidLoad() {
  //set default position of the mapView
  let camera = GMSCameraPosition.camera(withLatitude: 1.285, longitude: 10.848, zoom: 10)
  mapView.position = camera
  mapView.isMyLocationEnabled = true
  mapView.settings.compassButton = true
  mapView.settings.myLocationButton = true
  mapView.camera = camera
  mapView.delegate = self
 }