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

Ale*_*tev 7 google-maps ios swift

我使用google maps api for iOS.我想得到特殊城市的静态图像并将其粘贴到UIImageView中.我该怎么做?

Mar*_*coc 16

@Ankit的回复是对的,但@Alexsander在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)
Run Code Online (Sandbox Code Playgroud)

对于斯威夫特4

let staticMapUrl: String = "http://maps.google.com/maps/api/staticmap?markers=\(self.finalLatitude),\(self.finalLongitude)&\("zoom=15&size=\(2 * Int(imgViewMap.frame.size.width))x\(2 * Int(imgViewMap.frame.size.height))")&sensor=true"

let mapUrl: NSURL = NSURL(string: staticMapUrl)!
self.imgViewMap.sd_setImage(with: mapUrl as URL, placeholderImage: UIImage(named: "palceholder"))
Run Code Online (Sandbox Code Playgroud)


Ank*_*ava 5

    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];
Run Code Online (Sandbox Code Playgroud)

这应该有所帮助.