Search code examples
iosswiftuiimageviewmaps

not able to set map as image in uiimageview swift


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])

Above code is not working.


Solution

  • Try this. Note you have to get API key from Google cloud

    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"))
    

    Always check the link in browser to view whether it is working fine or not means the image is showing correctly without any Map error: g.co/staticmaperror. If you find any staticmaperror, please visit map-static-error-messages.

    Sample image:

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