我有一张正确显示的地图,我现在唯一要做的就是在加载时设置缩放级别。有没有办法做到这一点?
由于
答案 0 :(得分:181)
我发现自己是一个解决方案,这很简单并且可以解决问题。使用MKCoordinateRegionMakeWithDistance
可以垂直和水平设置以米为单位的距离,以获得所需的缩放。当然,当您更新您的位置时,您将获得正确的坐标,或者您可以在启动时直接在CLLocationCoordinate2D
中指定它,如果您需要这样做:
CLLocationCoordinate2D noLocation;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.showsUserLocation = YES;
夫特:
let location = ...
let region = MKCoordinateRegionMakeWithDistance(location.coordinate, CLLocationDistance(exactly: 5000)!, CLLocationDistance(exactly: 5000)!)
mapView.setRegion(mapView.regionThatFits(region), animated: true)
答案 1 :(得分:46)
基于经度线在地图的任何位置均匀间隔的事实,有一个非常简单的实现来设置centerCoordinate和zoomLevel:
@interface MKMapView (ZoomLevel)
@property (assign, nonatomic) NSUInteger zoomLevel;
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
zoomLevel:(NSUInteger)zoomLevel
animated:(BOOL)animated;
@end
@implementation MKMapView (ZoomLevel)
- (void)setZoomLevel:(NSUInteger)zoomLevel {
[self setCenterCoordinate:self.centerCoordinate zoomLevel:zoomLevel animated:NO];
}
- (NSUInteger)zoomLevel {
return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1;
}
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated {
MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256);
[self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:animated];
}
@end
答案 2 :(得分:29)
它没有内置,但我看过/使用过this代码。 这允许你使用它:
[mapView setCenterCoordinate:myCoord zoomLevel:13 animated:YES];
注意:这不是我的代码,我没有写,所以因此不能归功于它
答案 3 :(得分:16)
您还可以使用MKCoordinateRegion进行缩放并设置其范围纬度&经度三角洲。以下是快速参考,here是iOS参考。它不会做任何花哨的事情,但应该允许你在绘制地图时设置缩放。
MKCoordinateRegion region;
region.center.latitude = {desired lat};
region.center.longitude = {desired lng};
region.span.latitudeDelta = 1;
region.span.longitudeDelta = 1;
mapView.region = region;
修改1:
MKCoordinateRegion region;
region.center.latitude = {desired lat};
region.center.longitude = {desired lng};
region.span.latitudeDelta = 1;
region.span.longitudeDelta = 1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:TRUE];
答案 4 :(得分:10)
一个简单的Swift实现,如果你使用outlet。
@IBOutlet weak var mapView: MKMapView! {
didSet {
let noLocation = CLLocationCoordinate2D()
let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500)
self.mapView.setRegion(viewRegion, animated: false)
}
}
根据@ Carnal的回答。
答案 5 :(得分:7)
对于 Swift 3 ,这是非常快的:
private func setMapRegion(for location: CLLocationCoordinate2D, animated: Bool)
{
let viewRegion = MKCoordinateRegionMakeWithDistance(location, <#T##latitudinalMeters: CLLocationDistance##CLLocationDistance#>, <#T##longitudinalMeters: CLLocationDistance##CLLocationDistance#>)
MapView.setRegion(viewRegion, animated: animated)
}
只需定义lat-,long-Meters <CLLocationDistance>
,mapView就会使缩放级别适合您的值。
答案 6 :(得分:6)
基于@AdilSoomro的伟大answer。我想出了这个:
@interface MKMapView (ZoomLevel)
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
zoomLevel:(NSUInteger)zoomLevel
animated:(BOOL)animated;
-(double) getZoomLevel;
@end
@implementation MKMapView (ZoomLevel)
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated {
MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256);
[self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:animated];
}
-(double) getZoomLevel {
return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta));
}
@end
答案 7 :(得分:3)
我希望以下代码片段对您有帮助。
- (void)handleZoomOutAction:(id)sender {
MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.s pan.latitudeDelta/0.5, mapView.region.span.longitudeDelta/0.5));
[mapView setRegion:newRegion];
}
- (void)handleZoomInAction:(id)sender {
MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.span.latitudeDelta*0.5, mapView.region.span.longitudeDelta*0.5));
[mapView setRegion:newRegion];
}
您可以选择任意值而不是0.5来减小或增加缩放级别。我点击了两个按钮就使用了这些方法。
答案 8 :(得分:2)
我知道这是一个迟到的回复,但我只是想解决自己设置缩放级别的问题。 goldmine的答案很棒,但我发现它在我的应用程序中运行得不够好。
经过仔细检查,金矿表示“经度线在地图的任何一点均匀分布”。事实并非如此,实际上纬度线从-90(南极)到+90(北极)的距离相等。经度线在赤道处最宽处汇合,汇聚到极点处。
因此我采用的实施方法是使用纬度计算如下:
@implementation MKMapView (ZoomLevel)
- (void)setCenterCoordinate:(CLLocationCoordinate2D)coordinate
zoomLevel:(NSUInteger)zoom animated:(BOOL)animated
{
MKCoordinateSpan span = MKCoordinateSpanMake(180 / pow(2, zoom) *
self.frame.size.height / 256, 0);
[self setRegion:MKCoordinateRegionMake(coordinate, span) animated:animated];
}
@end
希望在这个晚期阶段有所帮助。
答案 9 :(得分:2)
使用NSUserDefaults的Swift 2.0答案可以保存和恢复地图的缩放和位置。
保存地图位置和缩放的功能:
func saveMapRegion() {
let mapRegion = [
"latitude" : mapView.region.center.latitude,
"longitude" : mapView.region.center.longitude,
"latitudeDelta" : mapView.region.span.latitudeDelta,
"longitudeDelta" : mapView.region.span.longitudeDelta
]
NSUserDefaults.standardUserDefaults().setObject(mapRegion, forKey: "mapRegion")
}
每次移动地图时运行该功能:
func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool)
{
saveMapRegion();
}
保存地图缩放和位置的功能:
func restoreMapRegion()
{
if let mapRegion = NSUserDefaults.standardUserDefaults().objectForKey("mapRegion")
{
let longitude = mapRegion["longitude"] as! CLLocationDegrees
let latitude = mapRegion["latitude"] as! CLLocationDegrees
let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let longitudeDelta = mapRegion["latitudeDelta"] as! CLLocationDegrees
let latitudeDelta = mapRegion["longitudeDelta"] as! CLLocationDegrees
let span = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
let savedRegion = MKCoordinateRegion(center: center, span: span)
self.mapView.setRegion(savedRegion, animated: false)
}
}
将此添加到viewDidLoad:
restoreMapRegion()
答案 10 :(得分:1)
迅速:
?scatterplot
locValue是您的坐标。
答案 11 :(得分:0)
基于quentinadam的answer
Swift 5.1
// size refers to the width/height of your tile images, by default is 256.0
// Seems to get better results using round()
// frame.width is the width of the MKMapView
let zoom = round(log2(360 * Double(frame.width) / size / region.span.longitudeDelta))
答案 12 :(得分:0)
MKMapView
扩展名(+浮点缩放级别精度):
import Foundation
import MapKit
extension MKMapView {
var zoomLevel: Double {
get {
return log2(360 * (Double(self.frame.size.width / 256) / self.region.span.longitudeDelta)) + 1
}
set (newZoomLevel){
setCenterCoordinate(coordinate:self.centerCoordinate, zoomLevel: newZoomLevel, animated: false)
}
}
private func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Double, animated: Bool) {
let span = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: 360 / pow(2, zoomLevel) * Double(self.frame.size.width) / 256)
setRegion(MKCoordinateRegion(center: coordinate, span: span), animated: animated)
}
}
答案 13 :(得分:-1)
在这里,我将我的答案及其用于 swift 4.2 的工作。