Маркер положения Infowindows для Google Maps iOS - PullRequest
0 голосов
/ 05 мая 2018

Я пытаюсь показать несколько маркеров на картах Google, до сих пор это работает, я освобождаю Infowindows.

В моем проекте у меня есть: - AppDelegate.swift - ViewController.swift - CustomInfoWindow.xib (две метки и одна кнопка) - CustomInfoWindow.swift (IB для надписей и снизу)

Детали:

ViewController.swift

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {


    // Properties
    var map:GMSMapView!
    var longitudes:[Double]!
    var latitudes:[Double]!
    var architectNames:[String]!
    var completedYear:[String]!

    override func viewDidLoad() {
        super.viewDidLoad()


        //
        latitudes = [48.8566667,41.8954656,51.5001524]
        longitudes = [2.3509871,12.4823243,-0.1262362]
        architectNames = ["Stephen Sauvestre","Bonanno Pisano","Augustus Pugin"]
        completedYear = ["1889","1372","1859"]
        //
        self.map = GMSMapView(frame: self.view.frame)
        self.view.addSubview(self.map)
        self.map.delegate = self

        // Add 3 markers
        for i in 0...2 {
            let coordinates = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
            let marker = GMSMarker(position: coordinates)
            marker.map = self.map
            marker.icon = UIImage(named: "pin")
            marker.infoWindowAnchor = CGPoint(x: 0.5, y: 0.2)
            marker.accessibilityLabel = "\(i)"
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



    class func instanceFromNib() -> CustomInfoWindow {
        return UINib(nibName: "CustomInfoWindow", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomInfoWindow
    }

    var tappedMarker = GMSMarker()
    //var infoWindow = CustomInfoWindow()


    var infoWindow = CustomInfoWindow()


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

        let index:Int! = Int(marker.accessibilityLabel!)
        infoWindow = ViewController.instanceFromNib()
        infoWindow.architectLbl.text = architectNames[index]
        infoWindow.completedYearLbl.text = completedYear[index]
        infoWindow.infoBtn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        self.view.addSubview(infoWindow)
        return false
    }

    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        return UIView()
    }


    @objc func buttonTapped(sender: UIButton) {
        print("Yeah! Button is tapped!")     
    }


 }

CustomInfoWindow.swift

import UIKit

class CustomInfoWindow: UIView {

    @IBOutlet var completedYearLbl: UILabel!
    @IBOutlet var architectLbl: UILabel!
    @IBOutlet weak var infoBtn: UIButton!

}

Проблема, с которой я столкнулся, заключается в том, что, когда я нажимаю на РЫНОК, он кажется фиксированным на экране, когда я ожидаю, что он появится на вершине рынка, и если я перемещу карту, то тоже перемещаемся.

См. Изображение

Любые предложения по улучшению кода для этой детали.

1 Ответ

0 голосов
/ 07 мая 2018

Вы не определили: mapView.projection.point, функции: didChange position и didTapAt

Вот обновленный код:

import UIKit
import GoogleMaps

class ViewController: UIViewController, GMSMapViewDelegate {

    // Properties
    var map:GMSMapView!
    var longitudes:[Double]!
    var latitudes:[Double]!
    var architectNames:[String]!
    var completedYear:[String]!


    override func viewDidLoad() {
        super.viewDidLoad()


        //
        latitudes = [48.8566667,41.8954656,51.5001524]
        longitudes = [2.3509871,12.4823243,-0.1262362]
        architectNames = ["Stephen Sauvestre","Bonanno Pisano","Augustus Pugin"]
        completedYear = ["1889","1372","1859"]
        //
        self.map = GMSMapView(frame: self.view.frame)
        self.view.addSubview(self.map)
        self.map.delegate = self

        // Add 3 markers
        for i in 0...2 {
            let coordinates = CLLocationCoordinate2D(latitude: latitudes[i], longitude: longitudes[i])
            let marker = GMSMarker(position: coordinates)
            marker.map = self.map

            marker.icon = UIImage(named: "new-pin")

            marker.accessibilityLabel = "\(i)"
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    class func instanceFromNib() -> CustomInfoWindow {
        return UINib(nibName: "CustomInfoWindow", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! CustomInfoWindow
    }

    var tappedMarker = GMSMarker()
    var infoWindow = CustomInfoWindow()


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

        let index:Int! = Int(marker.accessibilityLabel!)
        print(index)

        let location = CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude)

        tappedMarker = marker
        infoWindow.removeFromSuperview()

        infoWindow = ViewController.instanceFromNib()



        infoWindow.architectLbl.text = architectNames[index]
        infoWindow.completedYearLbl.text = completedYear[index]

        infoWindow.center = mapView.projection.point(for: location)

        infoWindow.center.y = infoWindow.center.y - 107
        infoWindow.infoBtn.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)

        self.view.addSubview(infoWindow)

        return false
    }

    func mapView(_ mapView: GMSMapView, markerInfoWindow marker: GMSMarker) -> UIView? {
        return UIView()
    }



    func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {

        let location = CLLocationCoordinate2D(latitude: tappedMarker.position.latitude, longitude: tappedMarker.position.longitude)
        infoWindow.center = mapView.projection.point(for: location)
        infoWindow.center.y = infoWindow.center.y - 107


    }


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

    }



    @objc func buttonTapped(sender: UIButton) {
        print("Yeah! Button is tapped!")

    }


 }

image

Удачного кодирования!

...