我正在尝试在UItableviewCell组件中实现谷歌地图。我这样做的方法是在protoype单元格中定义GMSMapView,然后使用dequeueReusableCell方法配置地图单元格。但是,我尝试应用的任何更改都会失败(例如添加标记,相机,缩放等)。有没有人有关于这个问题的任何信息?
代码参考:
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
}
答案 0 :(得分:0)
我为你添加了GMSMapView的插座,仅此而已。我跳过了initMap()
方法。
在ViewController
cellForRowAt
中,它适用于我。
let marker = GMSMarker(position: CLLocationCoordinate2D(latitude: 51.483682, longitude: -0.091991))
marker.map = cell?.subView
你的问题究竟是什么?
答案 1 :(得分:0)
执行以下步骤将MapView添加到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 } }
在" 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
注意:这里我给出了一些额外的代码,使用" DynamicView"添加自定义标记。如果您不想忘记从Google开发者帐户配置谷歌地图的必需设置,并将相应的密钥添加到您的appdelegate,例如
t want to add custom marker, you can skip those code. Also Don