在iOS的MapView中缩放和集中用户位置时遇到问题
我试图显示一张地图,它只捕获用户的纬度和经度坐标,然后放大地图上的用户。我能够捕获用户的坐标,但无法显示放大到用户位置的地图,也无法将地图居中显示在用户身上。不幸的是,我只能看到一张地图,上面显示的是来自远方的用户。我的相关代码如下: 我在.h文件中声明了类型为CLLocationCoordinate2D的在iOS的MapView中缩放和集中用户位置时遇到问题,ios,mkmapview,core-location,cllocationmanager,Ios,Mkmapview,Core Location,Cllocationmanager,我试图显示一张地图,它只捕获用户的纬度和经度坐标,然后放大地图上的用户。我能够捕获用户的坐标,但无法显示放大到用户位置的地图,也无法将地图居中显示在用户身上。不幸的是,我只能看到一张地图,上面显示的是来自远方的用户。我的相关代码如下: 我在.h文件中声明了类型为CLLocationCoordinate2D的userLoc。然后,我的.m文件中有以下内容: - (void)locationManager:(CLLocationManager *)manager didUpdateToLoc
userLoc
。然后,我的.m文件中有以下内容:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
userLoc = newLocation.coordinate;
[locationManager stopUpdatingLocation];
[self showCurrentLocation];
if (currentLocation != nil) {
userLongitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
userLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
NSLog(@"User Latitude is %@", userLatitude);
NSLog(@"User Longitude is %@", userLongitude);
}
然后,我使用以下方法显示实际地图:
-(void) showCurrentLocation {
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 55, 320, 425)];
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
region.center = userLoc;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.span=span;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setShowsUserLocation:YES];
[self.view addSubview:mapView];
}
正如我所说,我可以查看显示用户位置的地图,但地图既不会放大,也不会将地图居中显示给用户。有人知道为什么吗?非常感谢您的解决方案。我没意识到我离得那么近:-)没问题!有时候,最愚蠢的事情很难被看到,而另一双眼睛会有所帮助
-(void) showCurrentLocation {
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 55, 320, 425)];
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
region.center = userLoc;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.span=span;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setShowsUserLocation:YES];
[mapView setRegion:region animated:YES];
[self.view addSubview:mapView];
}