使用UItableviewCell实施Google Map [英] Implementing a Google Map with UItableviewCell

查看:87
本文介绍了使用UItableviewCell实施Google Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在UItableviewCell组件内部实现google map.我这样做的方法是在原型单元中定义一个GMSMapView,然后使用dequeueReusableCell方法配置地图单元.但是,我尝试应用的任何更改都会失败(例如添加标记,相机,缩放等).有人对此问题有任何信息吗?

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?

代码参考:

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


}

推荐答案

执行以下步骤,将MapView添加到TableViewCell

Do following steps to add MapView to TableViewCell

  1. 在自定义Tableviewcell中获取一个UIView,并将该类名称命名为"GMSmapView"给该UIView
  2. 确保连接Mapview的委托方法.
  3. 在您的ViewController中,导入"GoogleMaps"以及代理"GMSMapViewDelegate".
  4. 为您创建的TableViewCell设置MapView的出口:

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. 在"cellForRowAt"中添加以下代码

  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

注意:在这里,我给出了一些额外的代码,以使用"DynamicView"添加自定义标记.如果您不想添加自定义标记,则可以跳过这些代码.另外,不要忘记为Google开发者帐户中的Google地图配置要求设置,并向您添加相应的密钥,例如

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

这篇关于使用UItableviewCell实施Google Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆