我在iPhone应用中使用MKMapView
。单击按钮时,缩放级别必须增加。这是我的第一个方法:
MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.5;
[mapView setRegion:zoomIn animated:YES];
但是,这段代码没有效果,因为我没有更新longitudeDelta值。所以我添加了这一行:
zoomIn.span.longitudeDelta *= 0.5;
现在它有效,但有时只是。 latitudeDelta
和longitudeDelta
的变化方式不一样,我的意思是,它们的值不成比例。知道如何解决这个问题吗?
答案 0 :(得分:27)
我不知道这是否是正确的方法,但我正在使用它来放大和缩小。
case 0: { // Zoom In
//NSLog(@"Zoom - IN");
MKCoordinateRegion region;
//Set Zoom level using Span
MKCoordinateSpan span;
region.center=mapView.region.center;
span.latitudeDelta=mapView.region.span.latitudeDelta /2.0002;
span.longitudeDelta=mapView.region.span.longitudeDelta /2.0002;
region.span=span;
[mapView setRegion:region animated:TRUE];
}
break;
// Zoom Out
case 2: {
//NSLog(@"Zoom - OUT");
MKCoordinateRegion region;
//Set Zoom level using Span
MKCoordinateSpan span;
region.center=mapView.region.center;
span.latitudeDelta=mapView.region.span.latitudeDelta *2;
span.longitudeDelta=mapView.region.span.longitudeDelta *2;
region.span=span;
[mapView setRegion:region animated:TRUE];
}
答案 1 :(得分:23)
只是清理dkdarel的答案
// delta is the zoom factor
// 2 will zoom out x2
// .5 will zoom in by x2
- (void)zoomMap:(MKMapView*)mapView byDelta:(float) delta {
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span = mapView.region.span;
span.latitudeDelta*=delta;
span.longitudeDelta*=delta;
region.span=span;
[mapView setRegion:region animated:YES];
}
Swift代码:
func zoomMap(byFactor delta: Double) {
var region: MKCoordinateRegion = self.mapView.region
var span: MKCoordinateSpan = mapView.region.span
span.latitudeDelta *= delta
span.longitudeDelta *= delta
region.span = span
mapView.setRegion(region, animated: true)
}
答案 2 :(得分:22)
这是一个更简单的解决方案:
MKUserLocation *userLocation = mapView.userLocation;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance (
userLocation.location.coordinate, 50, 50);
[mapView setRegion:region animated:NO];
答案 3 :(得分:2)
这是我将地图移动到注释点并将其缩放得非常接近的方法。您可以轻松更改第CGFloat newLatDelta = 0.06f;
行
- (void)moveMapToAnnotation:(MKPointAnnotation*)annotation
{
CGFloat fractionLatLon = map.region.span.latitudeDelta / map.region.span.longitudeDelta;
CGFloat newLatDelta = 0.06f;
CGFloat newLonDelta = newLatDelta * fractionLatLon;
MKCoordinateRegion region = MKCoordinateRegionMake(annotation.coordinate, MKCoordinateSpanMake(newLatDelta, newLonDelta));
[map setRegion:region animated:YES];
}
答案 4 :(得分:2)
刚刚将dkardel的解决方案翻译成了swift:
@IBAction func zoomOutButtonClicked(sender: UIButton) {
let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*2, longitudeDelta: mapView.region.span.longitudeDelta*2)
let region = MKCoordinateRegion(center: mapView.region.center, span: span)
mapView.setRegion(region, animated: true)
}
@IBAction func zoomInButtonClicked(sender: UIButton) {
let span = MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta/2, longitudeDelta: mapView.region.span.longitudeDelta/2)
let region = MKCoordinateRegion(center: mapView.region.center, span: span)
mapView.setRegion(region, animated: true)
}
答案 5 :(得分:1)
我使用类似的代码,它似乎工作。可能发生的情况是,您的增量变化不足以导致缩放级别从一个谷歌缩放级别增加到下一个级别。这还取决于地图的初始状态,这可以解释为什么它是间歇性的 - 那么在用户按下缩放按钮之前如何设置地图和缩放级别呢?
您还可以查看regionThatFits方法,该方法将调整您提供的区域(名称来自内存,因为我没有苹果文档方便)。
答案 6 :(得分:1)
- (IBAction)btnZoomInPressed
{
MKCoordinateRegion region;
MKCoordinateSpan span;
region.center.latitude = lati;
region.center.longitude = longi;
span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta /2.0002;
span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta /2.0002;
region.span=span;
[viewMapSingleVenue setRegion:region animated:TRUE];
}
- (IBAction)btnZoomOutPressed
{
MKCoordinateRegion region;
MKCoordinateSpan span;
region.center.latitude = lati;
region.center.longitude = longi;
span.latitudeDelta=viewMapSingleVenue.region.span.latitudeDelta *2;
span.longitudeDelta=viewMapSingleVenue.region.span.longitudeDelta *2;
if(span.latitudeDelta < 200)
{
region.span=span;
[viewMapSingleVenue setRegion:region animated:TRUE];
}
}
答案 7 :(得分:1)
mapView.setRegion
方法存在问题
您可以通过mapView.camera.altitude
属性缩放地图,但不会设置动画:
mapView.camera.altitude *= 1.05
您可以创建新的相机对象并使用动画进行设置:
let currentCamera = mapView.camera
let newCamera: MKMapCamera
if #available(iOS 9.0, *) {
newCamera = MKMapCamera(lookingAtCenter: currentCamera.centerCoordinate, fromDistance: currentCamera.altitude * 2, pitch: currentCamera.pitch, heading: currentCamera.heading)
} else {
newCamera = MKMapCamera()
newCamera.centerCoordinate = currentCamera.centerCoordinate
newCamera.heading = currentCamera.heading
newCamera.altitude = currentCamera.altitude * 2
newCamera.pitch = currentCamera.pitch
}
mapView.setCamera(newCamera, animated: true)
答案 8 :(得分:0)
在Swift 4.2中
private func adjustScrollViewOrigin(animated: Bool) {
func adjust(for height: CGFloat) {
topConstraint.constant = height
headerView.frame.size.height = height
innerScrollView.frame.origin.y = height
}
switch direction {
case .up:
if animated {
UIView.animate(withDuration: 0.25) { adjust(for: self.collapsedHeaderHeight) }
} else { adjust(for: collapsedHeaderHeight) }
case .down:
if animated {
UIView.animate(withDuration: 0.25) { adjust(for: self.expandedHeaderHeight) }
} else { adjust(for: expandedHeaderHeight) }
case .none:
break
}
}
func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
adjustScrollViewInsets(animated: true)
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
adjustScrollViewInsets(animated: true)
}
答案 9 :(得分:0)
要缩小 5%,请将区域纬度/经度增量乘以 1.05
let region = MKCoordinateRegion(center: mapView.centerCoordinate, span: MKCoordinateSpan(latitudeDelta: mapView.region.span.latitudeDelta*1.05, longitudeDelta: mapView.region.span.longitudeDelta*1.05))
self.mapView.setRegion(region, animated: true)