tableView中的Google Map

时间:2016-08-05 16:55:48

标签: swift uitableview google-maps xib latitude-longitude

我在使用swift在表格视图单元格中显示谷歌地图时遇到了问题。我想基于此显示检入(纬度和经度)我想在表格视图中显示谷歌地图。如果没有可用的位置意味着我将获得null,我将从服务器获得经度和纬度。所以,在一个场景中,我得到了正确的答案,但在重新加载顶级时,我正在获取地图的时间和地点,其纬度和经度也为空。请指导我。

3 个答案:

答案 0 :(得分:5)

地图视图是一种昂贵的实例化视图。即使使用dequeueReusableCellWithIdentifier,它仍然会耗费大量资源。

我相信使用从经度/纬度组合生成静态地图图像的方法是最好的选择。

您可以查看here,您可以看到如何轻松地为许多流行的地图提供商(例如Google)构建API调用,以获取静态图片地图,例如:

http://maps.googleapis.com/maps/api/staticmap?center=40.714728,-73.998672&zoom=13&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true

另外值得一提的是,其中一些提供商可能存在限制(如果流量较高,Google可能需要API密钥)。所以经过一些研究后明智地选择。

答案 1 :(得分:1)

看起来你正面临两个问题

1)已经提到过,即使数据为零,也会出现#34;

2)性能问题(虽然这里没有提到)

第一个是由于细胞的出列。重新加载表时,将重用该单元格(不再创建)。

当您在

中返回行的单元格时
tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)

带有地图视图的单元格可能会返回,这就是您在那里获得不需要的地图的原因。你应该把它设为零以防止这种情况。

2:

在演出时,它不是在每个单元格中显示mapview的正确方法。另一种方法是从地图网址中制作图像

使用此

let mapUrl: String = NSURL(string:"YourMapURL")

let mapImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))

答案 2 :(得分:0)

    let latitude:Double = 17.3850
    let longitude:Double = 78.4867

    let imageURL = NSURL(string: "http://maps.googleapis.com/maps/api/staticmap?center=\(latitude),\(longitude)&zoom=12&scale=false&size=600x300&maptype=roadmap&format=png&visual_refresh=true")

    let imagedData = NSData(contentsOfURL: imageURL!)!

    staticMapImgView.image = UIImage(data: imagedData)