@IBOutlet var mapView: GMSMapView!
let baseURLGeocode = "https://maps.googleapis.com/maps/api/geocode/json?"
var lookupAddressResults: Dictionary<NSObject, AnyObject>!
var fetchedFormattedAddress: String!
var fetchedAddressLongitude: Double!
var fetchedAddressLatitude: Double!
override func viewDidLoad() {
super.viewDidLoad()
//Setup our Location Manager
let address = storeAddress.text
self.geocodeAddress(address, withCompletionHandler: { (status, success) -> Void in
if !success {
print(status)
if status == "ZERO_RESULTS" {
print("location not found")
}
}
else {
let coordinate = CLLocationCoordinate2D(latitude: self.fetchedAddressLatitude, longitude: self.fetchedAddressLongitude)
let camera = GMSCameraPosition(target: coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
//self.mapview.camera = camera
//self.mapview.animateToCameraPosition(camera)
//let mapView = GMSMapView.mapWithFrame(CGRect.zero, camera: camera)
self.mapView.camera = camera
self.mapView.animateToZoom(18)
self.mapView.setMinZoom(15, maxZoom: 25)
self.mapView.mapType = kGMSTypeTerrain;
self.mapView.delegate=self;
let marker = GMSMarker()
marker.position = camera.target
marker.snippet = address
marker.appearAnimation = kGMSMarkerAnimationPop
marker.map = self.mapView
let circ = GMSCircle.init(position: marker.position, radius: 50)
circ.fillColor = UIColor.blackColor().colorWithAlphaComponent(0.2)
circ.strokeColor = UIColor.redColor()
circ.strokeWidth = 1
circ.map = self.mapView
//self.viewMap.camera = GMSCameraPosition.cameraWithTarget(coordinate, zoom: 14.0)
}
})
self.overlay = AlertView.init(frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height))
}
func geocodeAddress(address: String!, withCompletionHandler completionHandler: ((status: String, success: Bool) -> Void)) {
if address == ""
{
completionHandler(status: "No valid address.", success: false)
}
else if let lookupAddress = address {
var geocodeURLString = baseURLGeocode + "address=" + lookupAddress
geocodeURLString = geocodeURLString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
let geocodeURL = NSURL(string: geocodeURLString)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
let geocodingResultsData = NSData(contentsOfURL: geocodeURL!)
//let json = try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
let dictionary: Dictionary<NSObject, AnyObject> = try! NSJSONSerialization.JSONObjectWithData(geocodingResultsData!, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<NSObject, AnyObject>
// Get the response status.
let status = dictionary["status"] as! String
if status == "OK" {
let allResults = dictionary["results"] as! Array<Dictionary<NSObject, AnyObject>>
self.lookupAddressResults = allResults[0]
// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as! String
let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
completionHandler(status: status, success: true)
}
else {
completionHandler(status: status, success: false)
}
})
}
else {
completionHandler(status: "No valid address.", success: false)
}
}
![enter image description here](https://i.stack.imgur.com/8Vlgk.png)
出典
2017-01-12 09:01:38
rvx
私が試した、これは私にとってはそうではありませんでした。そのボタンがMapviewの上に正しく配置されていることを確認して、segueがボタンから接続されているかどうかを確認する必要がありますか? – rvx
MapViewの上に配置する方法を確認するにはどうすればよいですか? –
MapViewの背後にUIViewがありません。これが起こるのだろうか? –