iOS编程中的中心地图 [英] Center maps in iOS Programming
问题描述
我们如何在地图中关注用户。我希望蓝点(用户位置)位于地图的中心,但我还要允许用户放大和缩小,然后在用户位置放大几秒后放大。
How do we follow the user in maps. I want to have the blue dot (user location) be in the center of the map, But I also what to allow the user to zoom in and zoom out and then after a couple seconds zoom in back in the user location.
我对解决方案的教育猜测:我们检测用户是否放大或缩小,在没有放大或缩小检测三秒后,我们开始关注用户:)。你的帮助会很棒:)
My Educated Guess for the Solution: We detect if the user is zooming in or out, after three seconds of no zooming in or out detection, we starting follow the user :). Your HELP would be awesome :)
这段代码放大了用户的位置,但没有延迟放大和缩小:
This code zoom in the user location but doesn't delay for zoom in and out:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0); [mapView setRegion:userLocation animated:YES];
}
推荐答案
<我做了一个小例子来说明如何将这个工作委托给Map SDK。
当然你可以听取位置变化,但MKUserTrackingModeFollow会自动为你做这个,所以只需一行代码
I made a little example to show how you can delegate this job to the Map SDK. Of course you could listen to the Location change but MKUserTrackingModeFollow automatically does this for you, so just a single line of code
#import <MapKit/MapKit.h>
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
//Always center the dot and zoom in to an apropriate zoom level when position changes
[mapView setUserTrackingMode:MKUserTrackingModeFollow];
//don't let the user drag around the the map -> just zooming enabled
[mapView setScrollEnabled:NO];
[self.view addSubview:mapView];
}
然后该应用程序如下所示:
Then the app looks like this:
有关详细信息,请阅读Apple文档:
http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class /MKMapView/MKMapView.html
For more information just read the Apple Documentation: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
这篇关于iOS编程中的中心地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!