xcode 转到/缩放到当前位置功能(MapKit)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7021539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Go to/zoom to current location function (MapKit)
提问by Jan
I've got a mapView which zooms to the current location using viewDidLoad
:
我有一个 mapView,它使用viewDidLoad
以下命令缩放到当前位置:
#define METERS_PER_MILE 1609.344
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.showsUserLocation=TRUE;
// zoom to a specific area
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = -28.994167;
zoomLocation.longitude = 134.866944;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
// make sure the Google water mark is always visible
mapView.autoresizingMask =
(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
[mapView setRegion:adjustedRegion animated:YES];
mapView.delegate=self;
searchBar.delegate = self;
}
This works fine. I've added a search bar and a function to jump to a specific address location. This works fine, too. I now want to add a button to jump back to the current location. Can you give me a hand, please?
这工作正常。我添加了一个搜索栏和一个跳转到特定地址位置的功能。这也很好用。我现在想添加一个按钮来跳回当前位置。请你帮我一下好吗?
Cheers
干杯
回答by Bushra Shahid
You need to set the center of your map to the current location on tap of that button. Say, like this:
您需要在点击该按钮时将地图的中心设置为当前位置。说,像这样:
- (IBAction)showCurrentLocation {
[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
回答by Jonathan Gurebo
you can also try:
你也可以尝试:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
回答by Philippe
You can link this IBAction to your UIButton, it's going to move the map on the current location and zoom on it.
您可以将此 IBAction 链接到您的 UIButton,它将在当前位置移动地图并对其进行缩放。
@IBOutlet weak var mapView: MKMapView!
@IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
if self.mapView != nil {
self.mapView.setRegion(MKCoordinateRegionMake(
self.mapView.userLocation.coordinate,
MKCoordinateSpanMake(0.1, 0.1)
), animated: true)
}
}
MKCoordinateSpan defines the area spanned by a map region, smaller these values are, closer you zoom on the map.
MKCoordinateSpan 定义了地图区域所跨越的区域,这些值越小,地图上的缩放越近。
回答by Nithin Yell
FOR SWIFT
为斯威夫特
Add this line in button action yourMKMapView.setUserTrackingMode(.follow, animated: true)
在按钮操作中添加这一行 yourMKMapView.setUserTrackingMode(.follow, animated: true)
make sure you add yourMKMapView.showsUserLocation = true
in viewDidLoad()
请确保您添加yourMKMapView.showsUserLocation = true
在viewDidLoad()
回答by pvllnspk
- (void)showCurrentLocation{
MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
[self.mapView setVisibleMapRect:zoomRect animated:YES];
}