[swift][google map][ios]郵便番号で地図を表示したい
2015/08/22
geocode
google map
swift
Swiftです。
郵便番号で検索して、地図を表示したいです。使っているのは、Google Mapです。
以前、javascriptで試したものをiosで実現したかったわけです。
どうもURLを直接たたく感じ
参考
ios - Using GMSAddress and GMSGeocoder to return Coordinates from Address in Swift - Stack Overflow
let BASE_URL_GEOCODE = "https://maps.googleapis.com/maps/api/geocode/json?"
func dispMapWithPostalCode(mapView:GMSMapView, pcode:String) {
var sGeocodeURL = "\(BASE_URL_GEOCODE)address=\(pcode)&country=jp®ion=JP&language=ja&sensor=false"
println("geocodeURL=\(sGeocodeURL)")
sGeocodeURL = sGeocodeURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let geocodeURL = NSURL(string: sGeocodeURL)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)
var error: NSError?
let dictionary: Dictionary<NSObject, AnyObject> = NSJSONSerialization.JSONObjectWithData(geocodingResultsData!, options: NSJSONReadingOptions.MutableContainers, error: &error) as! Dictionary<NSObject, AnyObject>
if (error != nil) {
println(error)
} else {
// Get the response status.
let status = dictionary["status"] as! String
if status == "OK" {
let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>>
let lookupAddressResults = allResults[0]
let geometry = lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
let lng = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
let lat = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
var camera = GMSCameraPosition.cameraWithLatitude(lat,
longitude: lng, zoom: 13)
mapView.camera = camera
} else {
// println("*** *** not ok")
}
}
})
}
func dispMapWithPostalCode(mapView:GMSMapView, pcode:String) {
var sGeocodeURL = "\(BASE_URL_GEOCODE)address=\(pcode)&country=jp®ion=JP&language=ja&sensor=false"
println("geocodeURL=\(sGeocodeURL)")
sGeocodeURL = sGeocodeURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
let geocodeURL = NSURL(string: sGeocodeURL)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)
var error: NSError?
let dictionary: Dictionary<NSObject, AnyObject> = NSJSONSerialization.JSONObjectWithData(geocodingResultsData!, options: NSJSONReadingOptions.MutableContainers, error: &error) as! Dictionary<NSObject, AnyObject>
if (error != nil) {
println(error)
} else {
// Get the response status.
let status = dictionary["status"] as! String
if status == "OK" {
let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>>
let lookupAddressResults = allResults[0]
let geometry = lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
let lng = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
let lat = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
var camera = GMSCameraPosition.cameraWithLatitude(lat,
longitude: lng, zoom: 13)
mapView.camera = camera
} else {
// println("*** *** not ok")
}
}
})
}
: