2016-01-18 10 views
5

Używam google maps api dla iOS. Chcę uzyskać statyczny obraz miasta specjalnego i wkleić go do UIImageView. Jak mogę to zrobić?Jak uzyskać obraz statyczny z mapy google w iOS

+0

Oto link do Objecive C, można spróbuj tego –

+0

możesz znaleźć coś tutaj, a następnie .. https://developers.google.com/maps/documentation/static-maps/?hl=pl –

Odpowiedz

8

odpowiedź od @Ankit ma rację, ale @Alexsander zapytał Swift, więc:

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

@ marok jak obliczyć zoom: ( – 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]; 

To powinno pomóc.

+0

Wiem tylko nazwa miasta – 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

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

jak obliczyć zoom :( – user3804063