简体   繁体   English

如何以特定位置为中心,然后缩放到当前位置

[英]How do I center over a specific location and then zoom to current location

Very much a newbie here so please forgive the ignorance. 这里有很多新手,请原谅。 I have spent some time trying to understand what I am missing but cannot figure it out. 我花了一些时间试图了解我所缺少的东西,但无法弄清楚。

My app centers over Washington State when loading but when I try to zoom to the users current location it puts me at latitude 0 longitude 0. If I comment out the "// startup: center over WA" section it centers over the users current location and then goToLocation works fine. 加载时,我的应用程序以华盛顿州为中心,但是当我尝试缩放到用户当前位置时,它的纬度为0经度0。如果我注释掉“ // startup:center over WA”部分,则它将以用户当前位置为中心然后goToLocation可以正常工作。

How do I get it to center over Washington State and then zoom to the users current location upon clicking goToLocation? 如何单击goToLocation使其位于华盛顿州的中心,然后缩放到用户当前位置?

Thanks! 谢谢!

- (void)viewDidLoad {

    [super viewDidLoad];
    [self loadOurAnnotations];
        [mapView setShowsUserLocation:NO];

// startup: center over WA
    CLLocationCoordinate2D defaultCoordinate;
    defaultCoordinate.latitude = 47.517201;
    defaultCoordinate.longitude = -120.366211;
    [mapView setRegion:MKCoordinateRegionMake(defaultCoordinate, MKCoordinateSpanMake(6.8, 6.8)) animated:NO];


}


-(IBAction)goToLocation {
    MKUserLocation *myLocation = [mapView userLocation];
    CLLocationCoordinate2D coord = [[myLocation location] coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 350, 350);
    [mapView setRegion:region animated:YES];

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:1];
    [UIView commitAnimations];
}

First, to use userLocation in the MKMapView at all, you have to pass YES to setShowsUserLocation (not NO ). 首先,要完全使用MKMapView中的userLocation ,必须将YES传递给setShowsUserLocation (不是NO )。

Next thing is that after turning showsUserLocation on, it may take a few seconds or more for the map view to determine the location and set userLocation . 接下来的事情是,在打开showsUserLocation之后,地图视图可能需要几秒钟或更长时间才能确定位置并设置userLocation Until then, the location will be nil (giving coordinates of 0,0). 在此之前,位置将为零(坐标为0,0)。

To really know when the userLocation is ready (or updated), implement the didUpdateUserLocation delegate method. 要真正知道userLocation准备就绪(或更新)的时间,请实现didUpdateUserLocation委托方法。 It's also helpful to implement the didFailToLocateUserWithError method in case of a problem determining the user location. 如果确定用户位置有问题,则实现didFailToLocateUserWithError方法也很有帮助。

However, in your case, you could just do the following in the goToLocation method: 但是,根据您的情况,您可以在goToLocation方法中执行以下goToLocation

MKUserLocation *myLocation = [mapView userLocation];
if (myLocation.location == nil)
{
    NSLog(@"user location has not been determined yet");
    return;
}
CLLocationCoordinate2D coord = [[myLocation location] coordinate];
MKCoordinateRegion region = ... //rest of the code stays the same

The animation statements at the end of that method don't do anything, by the way. 顺便说一句,该方法末尾的动画语句不执行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM