2016-01-18 2 views
5

Ich verwende Google Maps API für iOS. Ich möchte ein statisches Bild von einer speziellen Stadt bekommen und es in UIImageView einfügen. Wie kann ich es schaffen?Wie bekomme ich ein statisches Bild von Google Maps in iOS

+0

Hier Link für Objecive c, können Sie versuchen Sie diese –

+0

Sie können etwas finden Sie hier dann .. https://developers.google.com/maps/documentation/static-maps/?hl=en –

Antwort

8

die Antwort von @Ankit ist richtig, aber @Alexsander fragte 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) 
+0

@maroc wie den Zoom zu berechnen :( – user3804063

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

Dies sollte helfen.

+0

Ich kenne nur Stadt Name – Alexander

1
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

Swift Mit 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() 
} 
+0

wie man Zoom berechnet :( – user3804063

Verwandte Themen