Implementing a Google Map with UItableviewCell

白昼怎懂夜的黑 提交于 2019-12-11 05:18:04

问题


I am trying to implement a google map inside a UItableviewCell component. The way I am doing this is to define a GMSMapView within the protoype cell, and then using the dequeueReusableCell method i'm configuring the map cell. However, any change i try to apply fails (such as adding markers, camera, zoom, etc.). Does anybody have any information about this issue?

Code reference:

class UITenderInfoMapCell: UITableViewCell {

@IBOutlet weak var view: UIView!
@IBOutlet weak var subView: GMSMapView!

override func awakeFromNib() {

    super.awakeFromNib()
    self.initMap()

}



/**
    Init blank map when initializing a MapCell, waypoints, directions, etc can be loaded later.
**/
func initMap() {

    let camera = GMSCameraPosition.camera(withLatitude: 1.285, longitude: 103.848, zoom: 12)
    let mapView = GMSMapView.map(withFrame: .zero, camera: camera)
    self.subView = mapView


}

回答1:


Do following steps to add MapView to TableViewCell

  1. Take one UIView in your custom Tableviewcell and give the class name as "GMSmapView" to that UIView
  2. Make sure to connect delegate method of Mapview.
  3. In your ViewController import "GoogleMaps" along with delegate "GMSMapViewDelegate".
  4. Make outlet for MapView like this for your created TableViewCell:
import UIKit

import GoogleMaps

class MapviewTableViewCell: UITableViewCell {


@IBOutlet weak var mapviewObj: GMSMapView!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}

}
  1. Add Following code in "cellForRowAt"

            let cell = tableviewObj.dequeueReusableCell(withIdentifier: "cell") as! MapviewTableViewCell
    
            let marker = GMSMarker()
    
            let lat = Double("13.063754")
            let long = Double("80.24358699999993")
            marker.position = CLLocationCoordinate2DMake(lat!,long!)
    
            ///View for Marker
            let DynamicView = UIView(frame: CGRect(x:0, y:0, width:50, height:50))
            DynamicView.backgroundColor = UIColor.clear
    
            //Pin image view for Custom Marker
            let imageView = UIImageView()
            imageView.frame  = CGRect(x:0, y:0, width:50, height:35)
            imageView.image = UIImage(named:"LocationPin")
    
            //Adding pin image to view for Custom Marker
            DynamicView.addSubview(imageView)
    
            UIGraphicsBeginImageContextWithOptions(DynamicView.frame.size, false, UIScreen.main.scale)
            DynamicView.layer.render(in: UIGraphicsGetCurrentContext()!)
            let imageConverted: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
            UIGraphicsEndImageContext()
    
            marker.icon = imageConverted
            marker.map = cell.mapviewObj
    
            cell.mapviewObj.camera = GMSCameraPosition.camera(withTarget: marker.position, zoom: 11)
    
            return cell
    

NOTE:Here I am giving this some extra code to add your custom marker using "DynamicView". If you dont want to add custom marker, you can skip those code. Also Dont forget to configure require settings for google maps from google developer account and add respective key to you appdelegate e.g.

let key = "keep you key here"
GMSServices.provideAPIKey(key)



回答2:


I added an outlet for GMSMapView as you did, nothing more. I skipped your initMap() method.

And in ViewController cellForRowAt and it works fine for me.

let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: 51.483682, longitude: -0.091991))
marker.map = cell?.subView

What was your issue exactly?



来源:https://stackoverflow.com/questions/42338782/implementing-a-google-map-with-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!