How to get static image from google maps in iOS

I am using google maps api for iOS. I want to get a static image of a special city and paste it into a UIImageView. How can i do this?

+5
source share
4 answers

@Ankit's answer is right, but @Alexsander asked in Swift, therefore:

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) 
+8
source
  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.

+4
source
  Swift 3 => let Width = 100 let Height = 200 let mapImageUrl = "https://maps.googleapis.com/maps/api/staticmap?center=" let latlong = "18.495651759752, 73.809987567365" let mapUrl = mapImageUrl + latlong let size = "&size=" + "\(Int(Width))" + "x" + "\(Int(Height))" let positionOnMap = "&markers=size:mid|color:red|" + latlong let staticImageUrl = mapUrl + size + positionOnMap if let urlStr : NSString = staticImageUrl.addingPercentEscapes(using:String.Encoding.utf8)! as NSString{ // setImageFromURL } 
+1
source

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() } 
+1
source

Source: https://habr.com/ru/post/1240839/


All Articles