首页 > 解决方案 > 在 Google 地图自定义信息视图中显示 JSON 数据

问题描述

我在谷歌地图中有一些标记,并在地图上用 JSON 显示它们。有一个作为 UIView 的自定义信息视图可以在其中显示一些信息,但我无法在自定义信息视图标签中显示 json。标记也可以正常工作。我尝试使用数据提供程序来保存数据并在 didTapMarker 方法中使用它,但我无法处理它。

这是viewController

class maappp: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: GMSMapView!

    var tappedMarker = GMSMarker()

    var infoWindow = CustomInfoWindow(frame: CGRect(x: 0, y: 0, width: 300, height: 165))

    struct Lugares : Codable {

        let latitude : String?
        let longitude : String?
        let name : String?
        let address : String?
        let phone : String?

    }


    private var marcadores = [Lugares]()

    override func viewDidLoad() {


        super.viewDidLoad()

        self.mapView.delegate = self

        let cameraZone = GMSCameraPosition.camera(withLatitude: 35.688272, longitude: 51.386217, zoom: 11.0)
        mapView.camera = cameraZone

        let mapView = GMSMapView.map(withFrame: self.mapView.frame, camera: cameraZone)

        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
        mapView.settings.compassButton = true
        mapView.settings.indoorPicker = true
        mapView.settings.scrollGestures = true
        mapView.settings.zoomGestures = true

        self.tappedMarker = GMSMarker()

        guard let mapUrl = URL(string: LOCATION_BASE_URL) else { return }

        URLSession.shared.dataTask(with: mapUrl) { (data, response, error) in

            if error == nil {
                do {
                    self.marcadores = try JSONDecoder().decode([Lugares].self, from: data!)


                    DispatchQueue.main.async {

                        for state in self.marcadores {
                            print(state.latitude!, state.name!)

                            let lat = Double(state.latitude!)
                            let long = Double(state.longitude!)

             //markers work correctly 

                                    let marker = GMSMarker()
                                    marker.position = CLLocationCoordinate2D(
                                        latitude: lat!,
                                        longitude: long!)
                                    marker.map = self.mapView
                           marker.icon = UIImage(named: "place")

                        }


                    }





                } catch let jsonError{
                    print("An error occurred + \(jsonError)")
                }
            }
            }.resume()



    }

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

问题是如何将来自 json 的接收数据放在标签下方的 2 中,并在单击标记时显示主题

    func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
        let location = CLLocationCoordinate2D(latitude: marker.position.latitude, longitude: marker.position.longitude)
        self.tappedMarker = marker

        self.infoWindow.removeFromSuperview()
        self.infoWindow = maappp.instanceFromNib()



        self.infoWindow.nameLbl.text = ""
        self.infoWindow.AddressLbl.text = ""

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

        self.view.addSubview(self.infoWindow)
        return false
    }


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

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


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

这是 JSON 数组:

[
{
name: "name1",
longitude: "51.395836",
latitude: "35.776441",
address: "address1",
phone : "57767000"
},
{
name: "name2",
longitude: "51.247014",
latitude: "35.753798",
address: "address2",
phone : "66341933"
},
{
name: "name3",
longitude: "51.243728",
latitude: "35.755143",
address: "address3",
phone : "88887588"
}

]

标签: iosswiftgoogle-maps-markersgoogle-maps-sdk-ios

解决方案


推荐阅读