konyv12
konyv12

Reputation: 776

Why doesn't my Google Maps app not print out tapped coordinates?

I have the following code which if I compile it's not working: if I change zoom value, and recompile it again it's like "nothing changed". Also, when I tap, the print function does not print anything. What might be the issue?

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)
}

Upvotes: 0

Views: 69

Answers (1)

Andrew Walz
Andrew Walz

Reputation: 750

I believe your issue is that you are creating a second map view on the line:

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

and setting its delegate to self. You never add this map to the view. The map view you see and touch is from your storyboard which you have an outlet:

@IBOutlet weak var mapView: GMSMapView!

Which is not the same. You create a local variable with the same name. You never set this outlet's delegate. Instead of creating a new map view, simply set the camera position of the outlet:

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
 }

Upvotes: 1

Related Questions