0
A
回答
1
您可以通過MKCoordinateSpanMake
設置「縮放級別」。
試試這個:
mapView.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude), span: MKCoordinateSpanMake(0.05, 0.05)), animated: true)
0
不是我的代碼。也許它可以幫助你
extension MKMapView {
private var MERCATOR_OFFSET: Double {
get {
return 268435456
}
}
private var MERCATOR_RADIUS: Double {
get {
return 85445659.44705395
}
}
private var MAX_ZOOM_LEVEL: Double {
get {
return 19
}
}
// MARK: - Private functions
private func longitudeToPixelSpaceX (longitude: Double) -> Double {
return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI/180.0)
}
private func latitudeToPixelSpaceY (latitude: Double) -> Double {
let a = 1 + sinf(Float(latitude * M_PI)/180.0)
let b = 1.0 - sinf(Float(latitude * M_PI/180.0))/2.0
return round(MERCATOR_OFFSET - MERCATOR_RADIUS * Double(logf(a/b)))
}
private func pixelSpaceXToLongitude (pixelX: Double) -> Double {
return ((round(pixelX) - MERCATOR_OFFSET)/MERCATOR_RADIUS) * 180.0/M_PI
}
private func pixelSpaceYToLatitude (pixelY: Double) -> Double {
return (M_PI/2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET)/MERCATOR_RADIUS))) * 180.0/M_PI
}
private func coordinateSpanWithMapView(mapView: MKMapView, centerCoordinate: CLLocationCoordinate2D, andZoomLevel zoomLevel:Int) -> MKCoordinateSpan {
// convert center coordiate to pixel space
let centerPixelX = self.longitudeToPixelSpaceX(centerCoordinate.longitude)
let centerPixelY = self.latitudeToPixelSpaceY(centerCoordinate.latitude)
// determine the scale value from the zoom level
let zoomExponent = 20 - zoomLevel
let zoomScale = CGFloat(pow(Double(2), Double(zoomExponent)))
// scale the map’s size in pixel space
let mapSizeInPixels = mapView.bounds.size
let scaledMapWidth = mapSizeInPixels.width * zoomScale
let scaledMapHeight = mapSizeInPixels.height * zoomScale
// figure out the position of the top-left pixel
let topLeftPixelX = CGFloat(centerPixelX) - (scaledMapWidth/2)
let topLeftPixelY = CGFloat(centerPixelY) - (scaledMapHeight/2)
// find delta between left and right longitudes
let minLng: CLLocationDegrees = self.pixelSpaceXToLongitude(Double(topLeftPixelX))
let maxLng: CLLocationDegrees = self.pixelSpaceXToLongitude(Double(topLeftPixelX + scaledMapWidth))
let longitudeDelta: CLLocationDegrees = maxLng - minLng
// find delta between top and bottom latitudes
let minLat: CLLocationDegrees = self.pixelSpaceYToLatitude(Double(topLeftPixelY))
let maxLat: CLLocationDegrees = self.pixelSpaceYToLatitude(Double(topLeftPixelY + scaledMapHeight))
let latitudeDelta: CLLocationDegrees = -1 * (maxLat - minLat)
// create and return the lat/lng span
let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)
return span
}
// MARK: - Public Functions
func setCenterCoordinate(centerCoordinate: CLLocationCoordinate2D, zoomLevel: Int, animated: Bool) {
// clamp large numbers to 28
let zoom = min(zoomLevel, 28)
// use the zoom level to compute the region
let span = self.coordinateSpanWithMapView(self, centerCoordinate:centerCoordinate, andZoomLevel:zoom)
let region = MKCoordinateRegionMake(centerCoordinate, span)
// set the region like normal
self.setRegion(region, animated:animated)
}
func getZoomLevel() -> Int {
let longitudeDelta = self.region.span.longitudeDelta
let mapWidthInPixels = self.bounds.size.width*2 //2 is for retina display
let zoomScale = longitudeDelta * MERCATOR_RADIUS * M_PI/Double((180.0 * mapWidthInPixels))
var zoomer = MAX_ZOOM_LEVEL - log2(zoomScale)
if zoomer < 0 {
zoomer = 0
}
zoomer = round(zoomer)
return Int(zoomer)
}
}
使用像
mapView.setCenterCoordinate(mapView.centerCoordinate, zoomLevel: 17, animated: true)
0
這裏我加入圈子從當前位置到1公里半徑和地圖displaya約6平方公里的面積。我認爲它會幫助你。
// adding circle as overlay
func addRadiusCircle(location: CLLocation){
self.mapView.delegate = self
//radius of 1000 meters
let circle = MKCircle(centerCoordinate: location.coordinate, radius: 1000 as CLLocationDistance)
self.mapView.addOverlay(circle)
}
//circle design and coloring
func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
let circle = MKCircleRenderer(overlay: overlay)
if overlay is MKCircle {
circle.strokeColor = UIColor.blackColor()
circle.fillColor = UIColor(red: 235/255, green: 174/255, blue: 13/255, alpha:0.3)
circle.lineWidth = 1
}
return circle
}
// initial area to display on map
func centerMapOnLocation(location: CLLocation) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
6000, 6000)
mapView.setRegion(coordinateRegion, animated: true)
}
相關問題
- 1. 谷歌地圖半徑縮放級別
- 2. 如何爲地圖視圖設置縮放級別?
- 3. 如何在谷歌地圖中設置縮放級別
- 4. 如何設置地圖視圖的縮放級別,使三個針腳可見?
- 5. 如何爲離線地圖設置最大縮放級別?
- 6. 如何動態設置Google地圖自定義縮放級別?
- 7. 谷歌地圖 - 如何設置縮放級別
- 8. 在谷歌地圖android中動態設置縮放級別
- 9. 在谷歌Android地圖中自動設置縮放級別
- 10. 在谷歌地圖中設置自定義縮放級別
- 11. 如何在webview中設置修正縮放級別或縮小?
- 12. 按縮放級別改變半徑
- 13. Google靜態地圖半徑要縮放
- 14. 設置縮放級別在Google地圖上不起作用
- 15. 在谷歌地圖上設置適當的縮放級別
- 16. 如何在谷歌地圖中設置25公里的縮放級別?
- 17. 如何在Android中將最大縮放級別的Google地圖設置爲30?
- 18. 更改地圖中的縮放級別
- 19. 使用縮放級別動態更改Google地圖上的圓半徑
- 20. 如何在所有縮放級別中使用相同半徑在Google地圖v3中創建圈子?
- 21. 在視圖加載後在視圖中設置角點半徑
- 22. 如何將地圖縮放設置設置爲20 - Skobbler地圖
- 23. 谷歌地圖設置界限縮放級別
- 24. 設置世界風地圖的縮放級別
- 25. 根據顯示分辨率設置Google地圖縮放級別
- 26. iPhone:設置基於距離的地圖縮放級別
- 27. 如何根據標記位置設置Google地圖的縮放級別
- 28. 如何在新版Google地圖上設置默認縮放級別?
- 29. 如何在wordpress中增加谷歌地圖的縮放級別
- 30. 無法設置圖像縮放級別並修復圖像視圖