例如,如果注释是用户的西北部,则会缩小.但是,如果注释是用户的东南部,它只会缩小到足以让他们被切断,你必须手动缩小一点.这是我使用的代码,其他一些我在SO上发现的变体.
CLLocation *wheReiam = mapVIEw.userLocation.location; float lat = wheReiam.coordinate.latitude; float lon = wheReiam.coordinate.longitude; CLLocationCoordinate2D southWest = {[currentLatitude floatValue],[currentLongitude floatValue]}; CLLocationCoordinate2D northEast = southWest; southWest.latitude = MIN(southWest.latitude,lat); southWest.longitude = MIN(southWest.longitude,lon); northEast.latitude = MAX(northEast.latitude,lat); northEast.longitude = MAX(northEast.longitude,lon); CLLocation *locSouthWest = [[CLLocation alloc] initWithLatitude:southWest.latitude longitude:southWest.longitude]; CLLocation *locnorthEast = [[CLLocation alloc] initWithLatitude:northEast.latitude longitude:northEast.longitude]; CLLocationdistance meters = [locSouthWest distanceFromLocation:locnorthEast]; MKCoordinateRegion region; region.center.latitude = (southWest.latitude + northEast.latitude) / 2.0; region.center.longitude = (southWest.longitude + northEast.longitude) / 2.0; region.span.latitudeDelta = meters / 111319.5 region.span.longitudeDelta = 7.0; MKCoordinateRegion savedRegion = [mapVIEw regionThatFits:region]; [mapVIEw setRegion:savedRegion animated:YES]; [locSouthWest release]; [locnorthEast release];
有没有更好的方法内置MapKit只是缩小,以便两个注释,让他们说在他们之间的半帧在外框?我真的不在乎用户是否必须放大,我只是想要正确缩小.
解决方法-(voID)zoomToFitMapAnnotations:(MKMapVIEw*)mapVIEw{ if([mapVIEw.annotations count] == 0) return; CLLocationCoordinate2D topleftCoord; topleftCoord.latitude = -90; topleftCoord.longitude = 180; CLLocationCoordinate2D bottomrightCoord; bottomrightCoord.latitude = 90; bottomrightCoord.longitude = -180; for(MapAnnotation* annotation in mapVIEw.annotations) { topleftCoord.longitude = fmin(topleftCoord.longitude,annotation.coordinate.longitude); topleftCoord.latitude = fmax(topleftCoord.latitude,annotation.coordinate.latitude); bottomrightCoord.longitude = fmax(bottomrightCoord.longitude,annotation.coordinate.longitude); bottomrightCoord.latitude = fmin(bottomrightCoord.latitude,annotation.coordinate.latitude); } MKCoordinateRegion region; region.center.latitude = topleftCoord.latitude - (topleftCoord.latitude - bottomrightCoord.latitude) * 0.5; region.center.longitude = topleftCoord.longitude + (bottomrightCoord.longitude - topleftCoord.longitude) * 0.5; region.span.latitudeDelta = fabs(topleftCoord.latitude - bottomrightCoord.latitude) * 1.1; // Add a little extra space on the sIDes region.span.longitudeDelta = fabs(bottomrightCoord.longitude - topleftCoord.longitude) * 1.1; // Add a little extra space on the sIDes region = [mapVIEw regionThatFits:region]; [mapVIEw setRegion:region animated:YES];}
取自:http://codisllc.com/blog/zoom-mkmapview-to-fit-annotations/
(一直使用它.)
总结以上是内存溢出为你收集整理的ios – 在MapKit中放大并适合所有注释的最佳方式是什么?全部内容,希望文章能够帮你解决ios – 在MapKit中放大并适合所有注释的最佳方式是什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)