2017-02-08 3 views
0

Я чувствую, что делаю что-то действительно глупое, и ответ прямо передо мной. Я пробовал реализовать возможность нажимать на карту, которая создаст маркер в точке, нажатой, с названием адреса. Но всякий раз, когда я нажимаю на карту, ничего не происходит, нет никакой ошибки или чего-то еще. Я даже пытался использовать функцию longPress, но это тоже не работает. Может ли кто-нибудь посмотреть мой код и рассказать мне, что я делаю неправильно. Спасибо.Создание маркера с картами Google sdk swift

Мой импорт, locationManager и viewDidLoad

import UIKit 
import GoogleMaps 

class EventCreatorVC: UIViewController, GMSMapViewDelegate { 

@IBOutlet weak var SearchMap: UISearchBar! 
@IBOutlet weak var mapView: GMSMapView! 


let marker = GMSMarker() 

private lazy var locationManager: CLLocationManager = { 
    let locationManager = CLLocationManager() 
    locationManager.delegate = self 
    return locationManager 
}() 


override func viewDidLoad() { 
    super.viewDidLoad() 


    locationManager.delegate = self 
    mapView.accessibilityElementsHidden = false 
    mapView.settings.myLocationButton = true 
    locationManager.requestWhenInUseAuthorization() 
} 

Моя карта настроить:

private func configureMapView() { 
    mapView.delegate = self 

    // Center on California. 
    let camera = GMSCameraPosition.camera(withLatitude: 37.0, longitude: -120.0, zoom: 6.0) 
    mapView.camera = camera 

    mapView.settings.setAllGesturesEnabled(true) 
    mapView.settings.consumesGesturesInView = true 

    checkLocationAuthorizationStatus() 
    //addLocationMarkers() 
} 

2 функции у меня есть, чтобы попытаться заставить его работать, первое длинное нажатие и второй это просто крана.

func mapView(_ mapView: GMSMapView, didLongPressAt coordinate: CLLocationCoordinate2D) { 
let location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) 

    marker.position = location 

    self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker) 

    marker.map = mapView 
} 


func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { 

    let location = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) 

    marker.position = location 

    self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker) 

    marker.map = mapView 

} 

И мой обратный Geocoder

func reverseGeocodeCoordinate(coordinate: CLLocationCoordinate2D, marker: GMSMarker) { 

    // 1 
    let geocoder = GMSGeocoder() 
    geocoder.reverseGeocodeCoordinate(coordinate) { response, error in 

     //Add this line 


     //Rest of response handling 
    } 

    // 2 
    geocoder.reverseGeocodeCoordinate(coordinate) { response, error in 
     if let address = response?.firstResult() { 

      // 3 
      let title = address.lines as [String]? 
      marker.title = title?.first 

      UIView.animate(withDuration: 0.25) { 
       self.view.layoutIfNeeded() 
      } 
      } 
     } 
    } 
} 

ответ

1

Сделать userinteractionenabled к истинным, а также написать эту строку в viewdidload

let polyLine: GMSPolyline = GMSPolyline() 
    polyLine.isTappable = true 
    mapView.isUserInteractionEnabled = true 
    mapView.delegate = self 

А также изменить способ didTap с этим один

func mapView(_ mapView: GMSMapView, didTapAt coordinate: CLLocationCoordinate2D) { 
     locationOfMarker = CLLocationCoordinate2D(latitude: coordinate.latitude, longitude: coordinate.longitude) 

     marker.position = locationOfMarker 

     self.reverseGeocodeCoordinate(coordinate: coordinate, marker: marker) 

     marker.appearAnimation = kGMSMarkerAnimationPop 
    } 
+0

Это решило! Еще раз спасибо! – Willstarr