如何在没有InfoView的情况下在GMSMarker上添加tap事件

时间:2018-03-18 12:38:16

标签: swift google-maps google-places-api swift4 gmsmapview

我是Swift的初学者和来自日本的学生。因此我的英语不太好。但请阅读我的代码并给我解决方案。

我希望在点击GMSMarker时弹出详细视图 但是,当我点击GMSMarker时,将调用infoView,然后,infoView被点击,详细视图显示在此代码上。  我该如何修改? 是不是可以直接从GMSMarker直接看到没有infoView的详细视图?

请帮帮我。

//MARK: textfield
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    let autoCompleteController = GMSAutocompleteViewController()
    autoCompleteController.delegate = self

    let filter = GMSAutocompleteFilter()
    autoCompleteController.autocompleteFilter = filter

    self.locationManager.startUpdatingLocation()
    self.present(autoCompleteController, animated: true, completion: nil)
    return false
}

// MARK: GOOGLE AUTO COMPLETE DELEGATE
func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    let lat = place.coordinate.latitude
    let long = place.coordinate.longitude

    showPartyMarkers(lat: lat, long: long)

    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)
    myMapView.camera = camera
    txtFieldSearch.text=place.formattedAddress
    chosenPlace = restaurant(name: place.formattedAddress!, lat: lat, long: long)
    let restaurantMarker=GMSMarker()
    restaurantMarker.position = CLLocationCoordinate2D(latitude: lat, longitude: long)
    restaurantMarker.title = "\(place.name)"
    restaurantMarker.snippet = "\(place.formattedAddress!)"
    restaurantMarker.map = myMapView

    self.dismiss(animated: true, completion: nil) // dismiss after place selected
}

func viewController(_ viewController: GMSAutocompleteViewController, didFailAutocompleteWithError error: Error) {
    print("ERROR AUTO COMPLETE \(error)")
}

func wasCancelled(_ viewController: GMSAutocompleteViewController) {
    self.dismiss(animated: true, completion: nil)
}

func initGoogleMaps() {
    let camera = GMSCameraPosition.camera(withLatitude: 28.7041, longitude: 77.1025, zoom: 17.0)
    self.myMapView.camera = camera
    self.myMapView.delegate = self
    self.myMapView.isMyLocationEnabled = true
}

// MARK: CLLocation Manager Delegate

func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    print("Error while getting location \(error)")
}

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    locationManager.delegate = nil
    locationManager.stopUpdatingLocation()
    let location = locations.last
    let lat = (location?.coordinate.latitude)!
    let long = (location?.coordinate.longitude)!
    let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: long, zoom: 17.0)

    self.myMapView.animate(to: camera)

    showPartyMarkers(lat: lat, long: long)
}

// MARK: GOOGLE MAP DELEGATE restaurantMarkerがタップされた時の動き
func mapView(_ mapView: GMSMapView, didTap restaurantMarker: GMSMarker) -> Bool {
    guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return false }
    let img = customMarkerView.img!
    let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: img, borderColor: UIColor.white, tag: customMarkerView.tag)

    restaurantMarker.iconView = customMarker

    return false
}

func mapView(_ mapView: GMSMapView, markerInfoContents restaurantMarker: GMSMarker) -> UIView? {
    guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return nil }
    let data = previewDemoData[customMarkerView.tag]
    restaurantPreviewView.setData(title: data.title, img: data.img, price: data.price)
    return restaurantPreviewView
}

func mapView(_ mapView: GMSMapView, didTapInfoWindowOf restaurantMarker: GMSMarker) {
    guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return }
    let tag = customMarkerView.tag
    restaurantTapped(tag: tag)
}

func mapView(_ mapView: GMSMapView, didCloseInfoWindowOf restaurantMarker: GMSMarker) {
    guard let customMarkerView = restaurantMarker.iconView as? CustomMarkerView else { return }
    let img = customMarkerView.img!
    let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: img, borderColor: UIColor.darkGray, tag: customMarkerView.tag)
    restaurantMarker.iconView = customMarker
}
//マーカーがどう動くか
func showPartyMarkers(lat: Double, long: Double) {
    myMapView.clear()

    for restaurant in restaurants {
        let restaurantMarker=GMSMarker()
        let customMarker = CustomMarkerView(frame: CGRect(x: 0, y: 0, width: customMarkerWidth, height: customMarkerHeight), image: previewDemoData[0].img, borderColor: UIColor.darkGray, tag: 0)
        //[0]の部分をiにしてデータから順に引張てくるようにすれば良い
        restaurantMarker.position = CLLocationCoordinate2D(latitude: restaurant.lat, longitude: restaurant.long)
        restaurantMarker.iconView=customMarker
        restaurantMarker.map = self.myMapView
    }
}

@objc func btnMyLocationAction() {
    let location: CLLocation? = myMapView.myLocation
    if location != nil {
        myMapView.animate(toLocation: (location?.coordinate)!)
    }
}

@objc func restaurantTapped(tag: Int) {
    let v=DetailsVC()
    v.passedData = previewDemoData[tag]
    self.navigationController?.pushViewController(v, animated: true)
}

func setupTextField(textField: UITextField, img: UIImage){
    textField.leftViewMode = UITextFieldViewMode.always
    let imageView = UIImageView(frame: CGRect(x: 5, y: 5, width: 20, height: 20))
    imageView.image = img
    let paddingView = UIView(frame:CGRect(x: 0, y: 0, width: 30, height: 30))
    paddingView.addSubview(imageView)
    textField.leftView = paddingView
}

func setupViews() {
    view.addSubview(myMapView)
    myMapView.topAnchor.constraint(equalTo: view.topAnchor).isActive=true
    myMapView.leftAnchor.constraint(equalTo: view.leftAnchor).isActive=true
    myMapView.rightAnchor.constraint(equalTo: view.rightAnchor).isActive=true
    myMapView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 60).isActive=true

    self.view.addSubview(txtFieldSearch)
    txtFieldSearch.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 10).isActive=true
    txtFieldSearch.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 10).isActive=true
    txtFieldSearch.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -10).isActive=true
    txtFieldSearch.heightAnchor.constraint(equalToConstant: 35).isActive=true
    setupTextField(textField: txtFieldSearch, img: #imageLiteral(resourceName: "map_Pin"))

    restaurantPreviewView=RestaurantPreviewView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: 190))

    self.view.addSubview(btnMyLocation)
    btnMyLocation.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -30).isActive=true
    btnMyLocation.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive=true
    btnMyLocation.widthAnchor.constraint(equalToConstant: 50).isActive=true
    btnMyLocation.heightAnchor.constraint(equalTo: btnMyLocation.widthAnchor).isActive=true
}

let myMapView: GMSMapView = {
    let v=GMSMapView()
    v.translatesAutoresizingMaskIntoConstraints=false
    return v
}()

let txtFieldSearch: UITextField = {
    let tf=UITextField()
    tf.borderStyle = .roundedRect
    tf.backgroundColor = .white
    tf.layer.borderColor = UIColor.darkGray.cgColor
    tf.placeholder="場所で検索"
    tf.translatesAutoresizingMaskIntoConstraints=false
    return tf
}()

let btnMyLocation: UIButton = {
    let btn=UIButton()
    btn.backgroundColor = UIColor.white
    btn.setImage(#imageLiteral(resourceName: "my_location"), for: .normal)
    btn.layer.cornerRadius = 5
    btn.clipsToBounds=true
    btn.tintColor = UIColor.white
    btn.imageView?.tintColor=UIColor.white
    btn.addTarget(self, action: #selector(btnMyLocationAction), for: .touchUpInside)
    btn.translatesAutoresizingMaskIntoConstraints=false
    return btn
}()

var restaurantPreviewView: RestaurantPreviewView = {
    let v=RestaurantPreviewView()
    return v
}()

}

2 个答案:

答案 0 :(得分:0)

问题是您的标记是POI(兴趣点)标记,而不是自定义标记。您可以在POI标记(already answered on StackOverflow)上禁用infoViews,也可以将自己的标记添加到地图中,默认情况下不会打开infoViews。

自定义标记使用此引用marker的委托方法,而您引用的是restaurantMarker

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    ...
}

答案 1 :(得分:0)

每当点击GMSMarker时,您都会以GMSMapViewDelegate's方式获得回调:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool

默认情况下,点击GMSMarker时,会弹出该标记的infoWindow。如果您想在标记点按上执行其他操作,请将其添加到delegate方法和 return true

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
{
    //Your custom logic to move to another VC
    return true
}

你的问题陈述对我来说不是很清楚。如果您仍然遇到任何问题,请澄清。