如何在 swift 2 (xcode) 中使用 mapkit 放大某个位置
How do I zoom in on a location using mapkit in swift 2 (xcode)
我试图在用户输入地址时放大地图上的一个点,例如,一个无限循环。我想让它放大到街道级别的位置,但我不能为了上帝的爱让它放大。我试过弄乱区域、跨度等,但没有运气。这是我的代码的样子(我在视图中没有加载函数)...你会看到我已经注释掉了一些代码,因为我试过了但没有任何反应。
my code and here is what the output is on the simulator the output on simulator
为什么在geocodeAddressString
闭包中定义func
?
下面的代码怎么样?
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
print("Error", error)
}
if let placemark = placemarks?.first {
let span = MKCoordinateSpan(latitudeDelta: 0.0001, longitudeDelta: 0.0001)
let coordinate = placemark.location!.coordinate
let region = MKCoordinateRegion(center: coordinate, span: span)
self.mapView.setRegion(region, animated: true)
}
})
结果:
Use var
for span and region rather than let
.
You need to get rid of the self
in front of Map.setRegion
Also try a bigger number for the span coordinates something like 0.4
or 0.5
or you could still use 0.0001
that doesn't really matter.
span = MKCoordinateSpanMake(0.4,0.4)
var region = MKCoordinateRegion(center: **location variable goes in here** , span = span )
Map.setRegion(region, animated: true)
我试图在用户输入地址时放大地图上的一个点,例如,一个无限循环。我想让它放大到街道级别的位置,但我不能为了上帝的爱让它放大。我试过弄乱区域、跨度等,但没有运气。这是我的代码的样子(我在视图中没有加载函数)...你会看到我已经注释掉了一些代码,因为我试过了但没有任何反应。 my code and here is what the output is on the simulator the output on simulator
为什么在geocodeAddressString
闭包中定义func
?
下面的代码怎么样?
geocoder.geocodeAddressString(address, completionHandler: {(placemarks, error) -> Void in
if (error != nil) {
print("Error", error)
}
if let placemark = placemarks?.first {
let span = MKCoordinateSpan(latitudeDelta: 0.0001, longitudeDelta: 0.0001)
let coordinate = placemark.location!.coordinate
let region = MKCoordinateRegion(center: coordinate, span: span)
self.mapView.setRegion(region, animated: true)
}
})
结果:
Use
var
for span and region rather thanlet
.You need to get rid of the
self
in front ofMap.setRegion
Also try a bigger number for the span coordinates something like
0.4
or0.5
or you could still use0.0001
that doesn't really matter.
span = MKCoordinateSpanMake(0.4,0.4)
var region = MKCoordinateRegion(center: **location variable goes in here** , span = span )
Map.setRegion(region, animated: true)