如何在iOS中从谷歌地图获取静态图像

如何在iOS中从谷歌地图获取静态图像,ios,swift,google-maps,Ios,Swift,Google Maps,我在iOS上使用谷歌地图api。我想获得特殊城市的静态图像,并将其粘贴到UIImageView中。我怎样才能做到 NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:blue|%@,%@&%@&sensor=true",self.staticData.latitude, self.staticData.lo

我在iOS上使用谷歌地图api。我想获得特殊城市的静态图像,并将其粘贴到UIImageView中。我怎样才能做到

    NSString *staticMapUrl = [NSString stringWithFormat:@"http://maps.google.com/maps/api/staticmap?markers=color:blue|%@,%@&%@&sensor=true",self.staticData.latitude, self.staticData.longitude, [NSString stringWithFormat:@"zoom=13&size=%dx%d",2*(int)mapFrame.size.width, 2*(int)mapFrame.size.height]];
    NSURL *mapUrl = [NSURL URLWithString:[staticMapUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:mapUrl]];

    UIImageView *mapImage = [[UIImageView alloc] initWithFrame:mapFrame];

这应该会有帮助。

Ankit的回答是正确的,但是@Alexander用Swift提问,所以:

var staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:blue|\(self.staticData.latitude),\(self.staticData.longitude)&\("zoom=13&size=\(2 * Int(mapFrame.size.width))*\(2 * Int(mapFrame.size.height))")&sensor=true"
var mapUrl: NSURL = NSURL(string: staticMapUrl.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding))!
var image: UIImage = UIImage.imageWithData(NSData.dataWithContentsOfURL(mapUrl))
var mapImage: UIImageView = UIImageView(frame: mapFrame)
斯威夫特4号

使用Swift 3:

let lat = ..
let long = ..

let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:red|\(lat),\(long)&\("zoom=13&size=\(2 * Int(cell.imgAddress.frame.size.width))x\(2 * Int(cell.imgAddress.frame.size.height))")&sensor=true"

let url = URL(string: staticMapUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)

do {
    let data = try NSData(contentsOf: url!, options: NSData.ReadingOptions())
    cell.imgAddress.image = UIImage(data: data as Data)
} catch {
    cell.imgAddress.image = UIImage()
}

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


始终检查浏览器中的链接,以查看其是否正常工作,即图像中是否可见。

这是Objective c的链接,您可以尝试一下。您可能会在此处找到一些内容。。我只知道一个城市name@maroc如何计算缩放:(大小参数之间缺少“x”符号:size=100x100我尝试用这种方式打开我的链接,但应用程序在
let-mapUrl:NSURL=NSURL(string:staticMapUrl)!
这一点。你能告诉我为什么会发生这种情况吗。如何计算缩放:(
let lat = ..
let long = ..

let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=color:red|\(lat),\(long)&\("zoom=13&size=\(2 * Int(cell.imgAddress.frame.size.width))x\(2 * Int(cell.imgAddress.frame.size.height))")&sensor=true"

let url = URL(string: staticMapUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)

do {
    let data = try NSData(contentsOf: url!, options: NSData.ReadingOptions())
    cell.imgAddress.image = UIImage(data: data as Data)
} catch {
    cell.imgAddress.image = UIImage()
}
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"))