【问题标题】:not able to set map as image in uiimageview swift无法在 uiimageview swift 中将地图设置为图像
【发布时间】:2018-11-28 05:38:51
【问题描述】:
let staticURL : String = "https://www.google.com/maps/place/London,+UK/@51.528308,-0.3817765,10z/data=!3m1!4b1!4m5!3m4!1s0x47d8a00baf21de75:0x52963a5addd52a99!8m2!3d51.5073509!4d-0.1277583\("zoom=15&size=\(2 * Int(imgForMap.frame.size.width))x\(2 * Int(imgForMap.frame.size.height))")&sensor=true"
let mapURL = URL(string: staticURL)!
self.imgForMap.sd_setAnimationImages(with: [mapURL as URL])

以上代码不工作。

【问题讨论】:

  • 试试this回答!
  • @Dimple 我已经尝试过了,但它不适合我。
  • 你的意思是它不起作用?您能否更具体地说明哪行代码不起作用以及您遇到了什么错误?
  • 您的网址与共享答案不同。请检查正确的网址

标签: ios swift uiimageview maps


【解决方案1】:

试试这个。请注意,您必须从 Google cloud 获取 API 密钥

let API_Key = //Your API Key.
let url = "https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=\(2 * Int(imgBanner.frame.size.width))x\(2 * Int(imgBanner.frame.size.height))&maptype=roadmap&key=\(API_Key)"
let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgBanner.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))

始终检查浏览器中的链接以查看它是否工作正常意味着图像在没有任何Map error: g.co/staticmaperror 的情况下正确显示。如果您找到staticmaperror,请访问map-static-error-messages

示例图片:

https://maps.googleapis.com/maps/api/staticmap?center=Brooklyn+Bridge,New+York,NY&zoom=13&size=600x300&maptype=roadmap&key=AIzaSyAJgFSXOEIqBWIZ2vUcngjpX5fBBFmX6t0

【讨论】:

  • 我有一个网址。但在 imageview 上不可见。它在浏览器上运行良好。 https://maps.googleapis.com/maps/api/staticmap?center=&maptype=roadmap&path=color:0x000000%7Cweight:10%7Cenc:u~kzDw}fsMsPxMeHvFkDnC}@r@a@bDdCrElJtQtBE|@BeG|EjBpDjA|BqAhA{CjCj@bA&markers=color:green%7Clabel:P%7C30.7046,76.7179&markers=color:red%7Clabel:D%7C30.707998,76.703069&key=AIzaSyDdchM7apzEter6yaWfyQG-dF6AORh34MA&size=400x150
  • 您能否分享一下您如何下载图像数据并在 UIImageView 上显示图像的代码?
  • 我正在使用以下代码:let newString = imageurl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)! 之后,我将此字符串转换为 url,并在SDWebImage的帮助下在 imageview 上显示该 url
  • 您分享的网址有问题。当我尝试 https://maps.googleapis.com/maps/api/staticmap?center=&maptype=roadmap&path=color:0x000000|weight:10|enc:u~kzDw}fsMsPxMeHvFkDnC}@r@a@bDdCrElJtQtBE|@BeG|EjBpDjA|BqAhA{CjCj@bA&markers=color:green|label:P|30.7046,76.7179&markers=color:red|label:D|30.707998,76.703069&key=AIzaSyDdchM7apzEter6yaWfyQG-dF6AORh34MA&size=400x150 时它工作正常
  • 如果你看到这两个 url 都可以在浏览器上运行,但是如果我尝试将它设置在 UIImageView id 上会显示一些奇怪的东西。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多