Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get static image from google maps in iOS

I use google maps api for iOS. I want to get static image of special city and paste it in UIImageView. How can I make it?

like image 768
Alexander Khitev Avatar asked Jan 18 '16 09:01

Alexander Khitev


People also ask

Is Google maps static API free?

The Maps Static API uses a pay-as-you-go pricing model. Requests for the Maps Static API are billed under the SKU for Static Maps.

Is Google Maps static or dynamic?

What is a Static Web API? The Google Maps Platform static web APIs let you embed a Google Maps image on your web page without requiring JavaScript or any dynamic page loading. The APIs create an image based on URL parameters sent through a standard HTTP request and allow you to display the result on your web page.


4 Answers

the reply of @Ankit is right, but @Alexsander asked in Swift, so :

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)

for swift 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"))
like image 144
MarcoCarnevali Avatar answered Dec 03 '22 20:12

MarcoCarnevali


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

This should help.

like image 32
Ankit Srivastava Avatar answered Dec 03 '22 21:12

Ankit Srivastava


Using 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()
}
like image 34
Gilberto Santaniello Avatar answered Dec 03 '22 22:12

Gilberto Santaniello


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 in the image is visible or not.

like image 38
Sateesh Yemireddi Avatar answered Dec 03 '22 20:12

Sateesh Yemireddi